r - 更改 ggplot 中图例形状的填充

标签 r ggplot2

我有一个相当复杂的图,显示了我的数据中的三个不同变量 - 请参阅下面的示例图。我删除了其中一个图例,并尝试通过填充方 block 来更清楚地显示辅音颜色。我尝试过使用 guide_legend(override.aes()) 但我找不到调整颜色的方法。有办法做到吗?

示例数据为:

 exampledata <- tribble(~subject, ~group, ~time, ~consonant, ~type,  ~n,
                   "1", "A", 1, "p", F, 10,
                   "1", "A", 1, "t", T, 12,
                   "1", "A", 1, "k", T, 50,
                   "2", "A", 1, "p", T, 0,
                   "2", "A", 1, "t", T, 45,
                   "2", "A", 1, "k", F, 23,
                   "2", "A", 2, "p", F, 2,
                   "2", "A", 2, "t", T, 34,
                   "2", "A", 2, "k", T, 56,
                   "3", "B", 1, "p", F, 12,
                   "3", "B", 1, "t", T, 13,
                   "3", "B", 1, "k", F, 50,
                   "4", "A", 1, "p", T, 10,
                   "4", "A", 1, "t", F, 12,
                   "4", "A", 1, "k", T, 50,
                   "5", "B", 1, "p", T, 0,
                   "5", "B", 1, "t", T, 24,
                   "5", "B", 1, "k", F, 3,
                   "5", "B", 2, "p", F, 23,
                   "5", "B", 2, "t", F, 12,
                   "5", "B", 2, "k", T, 7,
                   "6", "A", 1, "p", F, 52,
                   "6", "A", 1, "t", F, 12,
                   "6", "A", 1, "k", T, 64
 )  

以及我迄今为止拥有的ggplot代码:

 exampleplot <- ggplot(exampledata, aes(x = time, y=n, 
                       fill=type, colour = consonant))  +  
   geom_col(lwd = 1, aes(linetype = group)) +
   scale_linetype_manual(values = c("A" = "solid", 
                                    "B" = "twodash"), guide = F) +
   facet_grid(~subject, scales = "free_x", switch = "x") + 
   scale_fill_manual(values=c("gray97", "gray77"), 
                     labels = c("Type 1", "Type 2")) +
   theme_bw(base_size = 18) + 
   theme(strip.text.x = element_text(angle=75),
         axis.text.x  = element_blank(),
         axis.ticks.x = element_blank(),
         panel.border =element_blank(),
         strip.background = element_blank())

 plot(exampleplot)

example plot

最佳答案

exampleplot <- ggplot(exampledata, aes(x = time, y=n, 
                       alpha=type, fill = consonant, color = consonant))  +  
   geom_col(lwd = 1, aes(linetype = group)) +
   scale_linetype_manual(values = c("A" = "solid", 
                                    "B" = "twodash"), guide = F) +
   facet_grid(~subject, scales = "free_x", switch = "x") + 
   scale_alpha_discrete(range=c(0.4,0.8), 
                    labels = c("Type 1", "Type 2")) +
   theme_bw(base_size = 18) + 
   theme(strip.text.x = element_text(angle=75),
         axis.text.x  = element_blank(),
         axis.ticks.x = element_blank(),
         panel.border =element_blank(),
         strip.background = element_blank())

 plot(exampleplot)

我在 aesalpha 中添加了 fill 以保持 Type 之间的差异。这是您想要的带有填充平方的输出吗?

enter image description here

关于r - 更改 ggplot 中图例形状的填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57393321/

相关文章:

r - 在 R 中打印没有行号的列表列表

r - dplyr : NSE in joins (by)

python 情节9: how to change color scale

r - 在使用 ggplot 创建的绘图中,90 个图例中只有 32 个出现在屏幕上

r - 替代 ggplot2 2.0 中已弃用的 axis.ticks.margin

r - ggplot2:在单行中具有不同变量的多个图,单个分组图例

git - 如果rstudio如何从git选项卡中获取文件

r - 在 R 中拆开每个类别只有一行的数据帧

r - 使用dplyr创建命名向量

r - ggplot时间序列间隔堆积条