te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>r - Add a continuous colorbar with stat_contour_filled - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - Add a continuous colorbar with stat_contour_filled - Stack Overflow

programmeradmin4浏览0评论

When I use "stat_contour_filled" to draw a figure the legends are discrete colors like this:

I would like to add a continuous colorbar like this:

How can I achieve it, here's my code:

ggpolar(pole = "N", max.lat = 90, min.lat = 30,
    land.fill.colour = "#D9D9D9", land.outline.colour = "#343434", 
    plt.lat.labels = FALSE) +
ggtitle("A") +
theme(plot.title = element_text(hjust = 0, family = "Arial", size = 22,face = "bold")) +
stat_contour_filled(data = change_ratio_df, 
                  aes(x = lon, y = lat, z = change_ratio), 
                  breaks = c(-2, -1.8, -1.6, -1.4, -1.2, -1, -0.8, -0.6, -0.4, -0.2, 0,
                             0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2)) + 
scale_fill_manual(values  = c("#053061", "#124580", "#1D5FA1", "#2D74B3", "#3A86BD", "#529AC7",
                            "#74B1D4", "#95C5DE", "#B1D4E6", "#CAE1ED", "#DFEBF2", "#EDF2F5",
                            "#FAF3ED", "#FCE8D9", "#FCD8C0", "#FAC0A2", "#F5A684", "#EB896C",
                            "#DB6C56", "#CC4F43", "#BD2F32", "#A81428", "#850C24", "#66001F" ))+  
geom_point(data = selected_points, aes(x = lon, y = lat), color = "#343434", size = 0.5) +
geom_path(data = line_df,aes(X, Y, group = L1),colour = "#343434") +
theme(
plot.background = element_rect(fill = NA, color = NA),  # 设置整个图的背景为无颜色
panel.background = element_rect(fill = NA, color = NA),  # 设置绘图区域背景为无颜色
legend.position = "right",
legend.key.width = unit(2, "line"),
legend.key.height = unit(0.5, "line"),
legend.title = element_text(family = "Arial", size = 12),
legend.text = element_text(size = 12),
axis.title = element_blank(),
axis.text = element_text(family = "Arial", size = 12),
#panel.grid.major = element_line(size=0.25,linetype='dashed',colour="black"),
axis.ticks = element_blank()
)

When I use "stat_contour_filled" to draw a figure the legends are discrete colors like this:

I would like to add a continuous colorbar like this:

How can I achieve it, here's my code:

ggpolar(pole = "N", max.lat = 90, min.lat = 30,
    land.fill.colour = "#D9D9D9", land.outline.colour = "#343434", 
    plt.lat.labels = FALSE) +
ggtitle("A") +
theme(plot.title = element_text(hjust = 0, family = "Arial", size = 22,face = "bold")) +
stat_contour_filled(data = change_ratio_df, 
                  aes(x = lon, y = lat, z = change_ratio), 
                  breaks = c(-2, -1.8, -1.6, -1.4, -1.2, -1, -0.8, -0.6, -0.4, -0.2, 0,
                             0.2, 0.4, 0.6, 0.8, 1, 1.2, 1.4, 1.6, 1.8, 2)) + 
scale_fill_manual(values  = c("#053061", "#124580", "#1D5FA1", "#2D74B3", "#3A86BD", "#529AC7",
                            "#74B1D4", "#95C5DE", "#B1D4E6", "#CAE1ED", "#DFEBF2", "#EDF2F5",
                            "#FAF3ED", "#FCE8D9", "#FCD8C0", "#FAC0A2", "#F5A684", "#EB896C",
                            "#DB6C56", "#CC4F43", "#BD2F32", "#A81428", "#850C24", "#66001F" ))+  
geom_point(data = selected_points, aes(x = lon, y = lat), color = "#343434", size = 0.5) +
geom_path(data = line_df,aes(X, Y, group = L1),colour = "#343434") +
theme(
plot.background = element_rect(fill = NA, color = NA),  # 设置整个图的背景为无颜色
panel.background = element_rect(fill = NA, color = NA),  # 设置绘图区域背景为无颜色
legend.position = "right",
legend.key.width = unit(2, "line"),
legend.key.height = unit(0.5, "line"),
legend.title = element_text(family = "Arial", size = 12),
legend.text = element_text(size = 12),
axis.title = element_blank(),
axis.text = element_text(family = "Arial", size = 12),
#panel.grid.major = element_line(size=0.25,linetype='dashed',colour="black"),
axis.ticks = element_blank()
)
Share Improve this question asked Feb 17 at 15:02 Ruijie WangRuijie Wang 633 bronze badges 1
  • 2 Could you please share some reproducible data using dput? – Quinten Commented Feb 17 at 15:36
Add a comment  | 

1 Answer 1

Reset to default 0

You can achieve your desired result (which according to the image is not a colorbar but a binned legend) by switching to guide_bins and by tweaking the legend using theme= and override.aes. Additionally I used the labels= argument of scale_fill_manual to label only each second break.

Note: The number of colors you specify differ from the number of breaks. Hence, not all colors will be used.

Using a minimal reprex based on the faithfuld dataset.

library(ggplot2)

set.seed(123)

faithfuld2 <- faithfuld
faithfuld2$density <- runif(nrow(faithfuld2), -2.2, 2.2)
v <- ggplot(faithfuld2, aes(waiting, eruptions, z = density))

v +
  stat_contour_filled(
    breaks = seq(-2.2, 2.2, .2)
  ) +
  scale_fill_manual(
    values = c(
      "#053061", "#124580", "#1D5FA1", "#2D74B3", "#3A86BD", "#529AC7",
      "#74B1D4", "#95C5DE", "#B1D4E6", "#CAE1ED", "#DFEBF2", "#EDF2F5",
      "#FAF3ED", "#FCE8D9", "#FCD8C0", "#FAC0A2", "#F5A684", "#EB896C",
      "#DB6C56", "#CC4F43", "#BD2F32", "#A81428", "#850C24", "#66001F"
    ),
    labels = \(x) ifelse(seq_along(x) %% 2 == 1, x, "")
  ) +
  guides(
    fill = guide_bins(
      theme = theme(
        legend.axis.line = element_blank()
      ),
      override.aes = list(linewidth = 0)
    )
  )

发布评论

评论列表(0)

  1. 暂无评论