【问题标题】:ggplot2 scale_fill_gradient() function not changing point colors Rggplot2 scale_fill_gradient()函数不改变点颜色R
【发布时间】:2018-11-25 01:01:03
【问题描述】:

我在 R 中使用 ggplot2 来创建地图。过去,我已经能够成功地使用 scale_fill_gradient() 函数来控制 geom_point 填充。但是,当我运行下面的代码(提供了示例表)时,我只是得到了所有黑点的地图。图例显示正确,但点永远不会改变颜色。我认为我想要的变量没有映射到填充美学,但我不知道为什么。提前谢谢!

(如果重要的话,我正在使用 tibble 包来定义表)

table = tibble(long = c(15.28, 15.29, 15.3, 15.31, 15.32), lat = c(-4.4, -4.39, -4.38, -4.37, -4.36), consumption = c(NA, 3, 54, 6, 8))

mapping = aes_string(x = 'long', y = 'lat', fill = 'consumption')

# define breaks, limits, colors

low = 'seashell'
high = 'tan3'
breaks = c(0, max(na.omit(table)[['consumption']]))
limits = breaks

# plot 

p <- ggplot() +
  # points 
  geom_point(mapping = mapping, data = table, alpha = 0.7, size = 4) +
  # point colors
  scale_fill_gradient(low = low, high = high, na.value = 'darkgrey', guide = 'colorbar', aesthetics = 'fill'
                      , breaks = breaks, limits = limits) +
  # title
  ggtitle('consumption') +
  # title formatting
  theme(plot.title = element_text(color = "red", size = 10, face = "bold", hjust=0),
        legend.position="bottom",
        legend.text=element_text(size=9),
        legend.title=element_text(size=9)) +
  # legend
  guides(fill=guide_colorbar(title='consumption')) +
  # get rid of axes, etc.
  theme(axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank()) +
  xlab('') +
  ylab('') +
  # make legend correct
  theme(legend.box = 'vertical') +
  # add max/min corresponding to map
  xlim(c(15.28, 15.38)) +
  ylim(c(-4.41, -4.30))

【问题讨论】:

  • 点通常用aes(..., color = ...)而不是fill着色
  • @JackBrookes 感谢您的回复!不过,根据我的经验,“颜色”美学是指点的轮廓,而“填充”美学是指内部的阴影。在过去经常更改点颜色时,我已经能够使用“填充”,所以我认为这不是问题。 ETA:我完全纠正了!谢谢你:)
  • 没错,但geom_point中使用的默认点没有轮廓,其颜色由color而不是fill控制。这可以根据您使用的形状进行更改
  • @JackBrookes 很高兴知道!谢谢。

标签: r ggplot2 gis heatmap ggmap


【解决方案1】:

如 cmets 中所述,您必须将填充更改为颜色。这是我实现它的方法:

library(tidyverse)
table = tibble(long = c(15.28, 15.29, 15.3, 15.31, 15.32), lat = c(-4.4, -4.39, -4.38, -4.37, -4.36), consumption = c(NA, 3, 54, 6, 8))

##Changed here to color
mapping = aes_string(x = 'long', y = 'lat', color = 'consumption') 

# define breaks, limits, colors

low = 'seashell'
high = 'tan3'
breaks = c(0, max(na.omit(table)[['consumption']]))
limits = breaks

# plot 

ggplot() +
    # points 
    geom_point(mapping = mapping,
               data = table, alpha = 0.7, size = 4) +
    # point colors
    #Change here to aesthetics = color
    scale_color_gradient(low = low, high = high, na.value = 'darkgrey', guide = 'colorbar', aesthetics = 'color'
                        , breaks = breaks, limits = limits) +
    # title
    ggtitle('consumption') +
    # title formatting
    theme(plot.title = element_text(color = "red", size = 10, face = "bold", hjust=0),
          legend.position="bottom",
          legend.text=element_text(size=9),
          legend.title=element_text(size=9)) +
    # legend
    guides(fill=guide_colorbar(title='consumption')) +
    # get rid of axes, etc.
    theme(axis.line = element_blank(),
          axis.text = element_blank(),
          axis.ticks = element_blank()) +
    xlab('') +
    ylab('') +
    # make legend correct
    theme(legend.box = 'vertical') +
    # add max/min corresponding to map
    xlim(c(15.28, 15.38)) +
    ylim(c(-4.41, -4.30))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    相关资源
    最近更新 更多