使用下面的代码,我试图制作一个具有顶部和底部面板/面的图形,同时按状态和类别进行分组.

由于这个互动分组,我想做三件我正在苦苦挣扎的事情:

  1. 将图例一个接一个地放在一起,而不是并排
  2. 将标签添加到状态图例,使其为"不包括"(对于0)和"包括"(对于1)
  3. 将线型更改为点线而不是虚线

代码:

d %>% 
  filter(outcome != "alcohol") %>% 
ggplot(., aes(x = wave, y = mean, group = interaction(as.factor(status), category),
              color = category, linetype = as.factor(status))) +
  geom_point(size = 2.5) +
  geom_line(linewidth = 1.5) +
  facet_wrap(~outcome,
             ncol = 1,
             scales = "free_y") +
  labs(x = "",
       y = "") +
  theme_minimal() +
  theme(legend.position = "bottom")

生效日期:

d <- structure(list(wave = c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 2, 
2, 3, 3, 4, 4, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 1, 2, 2, 
3, 3, 4, 4, 5, 5, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5), mean = c(2.068, 
2.221, 2.044, 2.225, 1.965, 2.122, 2.269, 2.52, 2.416, 2.666, 
2.042, 2.237, 2.006, 2.256, 1.94, 2.176, 2.187, 2.5, 2.348, 2.668, 
2.053, 2.132, 2.069, 2.11, 1.998, 2.006, 2.4, 2.482, 2.593, 2.639, 
2.038, 2.189, 2.012, 2.177, 1.945, 2.127, 2.228, 2.5, 2.639, 
2.609, 2.166, 2.31, 2.132, 2.362, 2.067, 2.121, 2.458, 2.57, 
2.624, 2.815), category = c("all", "all", "all", "all", "all", 
"all", "all", "all", "all", "all", "white", "white", "white", 
"white", "white", "white", "white", "white", "white", "white", 
"black", "black", "black", "black", "black", "black", "black", 
"black", "black", "black", "nopoverty", "nopoverty", "nopoverty", 
"nopoverty", "nopoverty", "nopoverty", "nopoverty", "nopoverty", 
"nopoverty", "nopoverty", "poverty", "poverty", "poverty", "poverty", 
"poverty", "poverty", "poverty", "poverty", "poverty", "poverty"
), status= c(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

推荐答案

不完全是你正在寻找的一切,但要求2和3固定和堆叠的标题顶部的图例是一个略好于原来的.

d %>% 
   ggplot(., aes(x = wave, y = mean, group = interaction(as.factor(status), category),
                 color = category, linetype = as.factor(status))) +
   geom_point(size = 2.5) +
   geom_line(linewidth = 1.5) +
   facet_wrap(d$status, ncol = 1, scales = "free_y") +
   labs(x = "",  y = "") +
   theme_minimal() +
   theme(legend.position = "bottom") +
   guides(linetype = guide_legend(title.position = "top", keywidth = unit(1.5, "cm")), color=guide_legend(title.position = "top")) +
   scale_linetype_manual(values=c("solid", "dotted"), labels=c("Not Included", "Included"), name="scale")

enter image description here

R相关问答推荐

通过Plotly绘制线串几何形状的3D图

R等效于LABpascal(n,1)不同的列符号

使用tidyverse / Mutate的存款账户余额

找出疾病消失的受试者

使用tidyverse方法绑定行并从一组管道列表执行左连接

如何通过Docker部署我的shiny 应用程序(多个文件)

Rplotly中的Sankey Diagram:意外连接&

R函数‘paste`正在颠倒其参数的顺序

`lazy_dt`不支持`dplyr/across`?

当我们有多个反斜杠和/特殊字符时使用Gsubing

从R中的对数正态分布生成随机数的正确方法

按时间顺序对不同事件进行分组

是否有新方法来更改Facet_WRAP(Ggplot2)中条文本的文本 colored颜色 ?

KM估计的差异:SvyKm与带权重的调查

我如何go 掉盒子图底部的数字?

'使用`purrr::pwalk`从嵌套的嵌套框架中的列表列保存ggplots时出现未使用的参数错误

在具有多个响应变量的比例堆叠条形图上方添加总计

策略表单连接两个非常大的箭头数据集,而不会 destruct 内存使用

无法保存gglot的所有pdf元素

GgHighlight找不到它创建的列:`Highlight..1`->;`Highlight.....`