首页 > 解决方案 > 添加 scale_fill_grey 会更改条形,但不会更改 ggplot2 中的线

问题描述

我的数据如下:

counts <- structure(list(ECOST = c("0.08", "0.19", "0.07", "0.21", "0.06", 
"0.20", "0.08", "0.19", "0.07", "0.21", "0.06", "0.20", "0.08", 
"0.19", "0.07", "0.21", "0.06", "0.20", "0.08", "0.19", "0.07", 
"0.21", "0.06", "0.20"), variable = structure(c(2L, 2L, 2L, 2L, 
2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L), .Label = c("treatment", "control"), class = "factor"), 
    percentage = c(0.714495882305909, 0.769654513612482, 0.803015075376884, 
    0.778858962374387, 0.824455825864277, 0.743992307222663, 
    0.731115523760302, 0.767143914637009, 0.775154121121069, 
    0.766219224551454, 0.800973964325206, 0.767750444842938, 
    0.281818181818182, 0.418181818181818, 0.5, 0.354545454545455, 
    0.5, 0.372727272727273, 0.329896907216495, 0.43298969072165, 
    0.536082474226804, 0.402061855670103, 0.597938144329897, 
    0.443298969072165), type = c("Contributions", "Contributions", 
    "Contributions", "Contributions", "Contributions", "Contributions", 
    "Contributions", "Contributions", "Contributions", "Contributions", 
    "Contributions", "Contributions", "Choices", "Choices", "Choices", 
    "Choices", "Choices", "Choices", "Choices", "Choices", "Choices", 
    "Choices", "Choices", "Choices"), group = c("Contributions control", 
    "Contributions control", "Contributions control", "Contributions control", 
    "Contributions control", "Contributions control", "Contributions treatment", 
    "Contributions treatment", "Contributions treatment", "Contributions treatment", 
    "Contributions treatment", "Contributions treatment", "Choices control", 
    "Choices control", "Choices control", "Choices control", 
    "Choices control", "Choices control", "Choices treatment", 
    "Choices treatment", "Choices treatment", "Choices treatment", 
    "Choices treatment", "Choices treatment")), row.names = c(NA, 
-24L), class = c("data.table", "data.frame"))

library(ggplot2)
ggplot() +
  geom_bar(data=filter(counts, group %in% c("Choices control", "Choices treatment")), aes(x = ECOST, y = percentage, fill=group), stat ="identity", position="dodge")+
  geom_point(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")),aes(x = ECOST, y = percentage,colour=group)) +
  geom_line(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")), aes(x = ECOST, y = percentage,colour=group, group=group)) +
  theme_bw(base_size = 15)

创建此图:

在此处输入图像描述

我想制作一个使用灰色阴影的版本。但是,当我这样做时:

ggplot() +
  geom_bar(data=filter(counts, group %in% c("Choices control", "Choices treatment")), aes(x = ECOST, y = percentage, fill=group), stat ="identity", position="dodge")+
  geom_point(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")),aes(x = ECOST, y = percentage,colour=group)) +
  geom_line(data=filter(counts, group %in% c("Contributions control", "Contributions treatment")), aes(x = ECOST, y = percentage,colour=group, group=group)) +
  theme_bw(base_size = 15) +
  scale_fill_grey(start = 0.8, end = 0.5)

只有条改变,而不是线条。我如何改变线条?

在此处输入图像描述

标签: rggplot2

解决方案


添加scale_color_grey()将使线条颜色变为灰色阴影,例如

ggplot() +
  geom_bar(data=subset(counts, group %in% c("Choices control", "Choices treatment")), aes(x = ECOST, y = percentage, fill=group), stat ="identity", position="dodge")+
  geom_point(data=subset(counts, group %in% c("Contributions control", "Contributions treatment")),aes(x = ECOST, y = percentage,colour=group)) +
  geom_line(data=subset(counts, group %in% c("Contributions control", "Contributions treatment")), aes(x = ECOST, y = percentage,colour=group, group=group)) +
  theme_bw(base_size = 15) +
  scale_fill_grey(start = 0.8, end = 0.5)+
  scale_color_grey()

在此处输入图像描述 scale_fill_grey()将以灰色阴影渲染箱形图、条形图、小提琴图等,同时scale_color_grey()用于线条和点。


推荐阅读