【问题标题】:How to plot conditional colored outlines of barplots according to some variable?如何根据某些变量绘制条形图的条件彩色轮廓?
【发布时间】:2021-02-18 11:46:23
【问题描述】:

我有以下数据

structure(list(date = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), .Label = c("2011", 
"2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", 
"2020"), class = c("ordered", "factor")), station = c("41B001", 
"41B011_41R012", "41B001", "41B011_41R012", "41B001", "41B011_41R012", 
"41B001", "41B011_41R012", "41B001", "41B011_41R012", "41B001", 
"41B011_41R012", "41B001", "41B011_41R012", "41B001", "41B011_41R012", 
"41B001", "41B011_41R012", "41B001", "41B011_41R012"), concentration = c(NA, 
26.7276362390038, NA, 25.6793849658314, NA, 26.4231406374957, 
NA, 22.318982275586, NA, 22.0774877184965, NA, 21.6359649122807, 
56.1669215086646, 21.6140621430203, 56.1504197761194, 19.7031357486815, 
51.5015359168242, 17.0787333944114, 36.3595993515516, 11.4634841061866
), yield = c(0, 99.9200913242009, 0, 99.9544626593807, 0, 99.9200913242009, 
0, 99.8287671232877, 0, 99.9200913242009, 6.65983606557377, 99.931693989071, 
89.5890410958904, 99.9315068493151, 97.8995433789954, 99.5662100456621, 
96.62100456621, 99.6803652968037, 98.3151183970856, 99.9203096539162
), environ = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Urbain avec très faible influence du trafic", 
"Urbain avec très forte influence du trafic"), class = "factor"), 
    environ_station = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 2L, 
    1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Urbain avec très faible influence du trafic (41B011, 41R012)", 
    "Urbain avec très forte influence du trafic (41B001)"), class = "factor")), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

它是用下面的代码绘制的

ggplot(data, aes(x = date, y = concentration, fill = environ_station)) +
  geom_col(width = 0.75, colour = ifelse(round(data$yield, 0) < 85, "red", "black"), size = 0.5, position = position_dodge2(preserve = "single")) + guides(fill = guide_legend(nrow = 2)) +
  geom_hline(aes(yintercept = 40), linetype = 'dashed', colour = 'red', size = 1) +
  labs(x = '', y = label_conc) +
  theme_minimal() + theme(legend.position="bottom", legend.title = element_blank(), legend.margin=margin(l = -2, unit='line'),
                          legend.text = element_text(size = 10),
                          axis.text.y = element_text(size = 10), axis.title.y = element_text(size = 10), 
                          axis.text.x = element_text(size = 10), axis.title.x = element_blank(),
                          panel.grid.major.x = element_blank()) + geom_hline(yintercept = 0)

如图

但是,截至 2016 年的条形图的最终轮廓以红色绘制,而它们预计为黑色,因为它们的产量高于 85。似乎第二类中缺失的条形图(浓度 = NA)导致问题(订单混淆)。

请问有什么办法解决这个问题吗?谢谢。

【问题讨论】:

    标签: r ggplot2 colors colorbar geom-col


    【解决方案1】:

    问题是您通过颜色参数设置边框颜色。为了解决您的问题,在aes() 内有条件地设置颜色并使用scale_color_manual,就像我在下面的方法中所做的那样,或者使用scale_color_identity

    注意:我通过guides 删除了生成的颜色图例。

    library(ggplot2)
    
    ggplot(data, aes(x = date, y = concentration, fill = environ_station)) +
      geom_col(aes(colour = ifelse(round(yield, 0) < 85, "red", "black")), width = 0.75, 
               size = 0.5, position = position_dodge2(preserve = "single")) + 
      scale_color_manual(values = c(red = "red", black = "black")) +
      guides(fill = guide_legend(nrow = 2), color = FALSE) +
      geom_hline(aes(yintercept = 40), linetype = 'dashed', colour = 'red', size = 1) +
      #labs(x = '', y = label_conc) +
      theme_minimal() + theme(legend.position="bottom", legend.title = element_blank(), legend.margin=margin(l = -2, unit='line'),
                              legend.text = element_text(size = 10),
                              axis.text.y = element_text(size = 10), axis.title.y = element_text(size = 10), 
                              axis.text.x = element_text(size = 10), axis.title.x = element_blank(),
                              panel.grid.major.x = element_blank()) + geom_hline(yintercept = 0)
    #> Warning: Removed 6 rows containing missing values (geom_col).
    

    【讨论】:

    • 对此解决方案稍作修改,因为如果填充变量可以采用超过 2 个不同的值,则条形将根据颜色变量排序,然后根据填充变量排序,这可能导致每个不同的顺序年。一个解决方案是在 ggplot 调用中使用填充美学转向颜色美学:ggplot(data, aes(x = date, y = concentration, fill = environ_station, colour = ifelse(round(yield, 0) &lt; 85, "red", "black"))) + geom_col(width = 0.75, size = 0.5, position = position_dodge2(preserve = "single")) + ...
    猜你喜欢
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 2015-07-29
    相关资源
    最近更新 更多