【问题标题】:Add lines to existing ggplot and source them in legend将线条添加到现有的 ggplot 并在图例中获取它们
【发布时间】:2019-09-28 22:52:14
【问题描述】:

我正在尝试为已添加到现有 ggplot 的行添加一个键。

设置:

test.mat <- data.frame(matrix(nrow=32, ncol =3))
test.mat[,1] = rep(1:16,2)
test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
test.mat[,3] = rnorm(32,0,1)
colnames(test.mat) = c("Window", "type", "value")

ggplot(test.mat, aes(x=Window, y=value, size = type)) +
  geom_line(aes(colour = type, linetype = type)) +
  scale_colour_manual("type", values = c("black", "steelblue")) +
  scale_linetype_manual("type", values = c("solid", "solid")) +
  scale_size_manual("type", values = c(0.8, 1.4), guide = "none") +
  xlab("Quarter after alpha assessment") + ylab("t-statistics") + ggtitle("Panel Regression t-statistics") +  theme_classic() + 
  theme(
    text = element_text(family = "Times New Roman"),
    plot.title = element_text(color = "black", size = 12, hjust = 0.5),
    axis.title = element_text(size = 12),
    legend.title = element_blank()) +
    geom_hline(yintercept = 1.282 , color = "dark green") +
    geom_hline(yintercept = 1.645 , color = "dark grey") +
    geom_hline(yintercept = 2.326 , color = "grey")

我想要标记为“*”的深绿色线,我想要标记为“**”的深灰色线,并且我想要标记为“***”的灰色线。我希望这些标签出现在图例中。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以在geom_hlineaes 中使用linetype,以便将这些行包含在图例中。我更改了scale_linetype_manual,所以它会反映新图例线条的颜色。这是你的想法吗?

    library(ggplot2)
    
    set.seed(1)
    
    test.mat <- data.frame(matrix(nrow=32, ncol =3))
    test.mat[,1] = rep(1:16,2)
    test.mat[1:16,2] = as.character(rep("Cohort Alpha"),16)
    test.mat[17:32,2] = as.character(rep("Factor Alpha"), 16)
    test.mat[,3] = rnorm(32,0,1)
    colnames(test.mat) = c("Window", "type", "value")
    
    ggplot(test.mat, aes(x=Window, y=value, size = type)) +
      geom_line(aes(colour = type)) +
      scale_colour_manual("type", values = c("black", "steelblue")) +
      scale_size_manual("type", values = c(0.8, 1.4), guide = "none") +
      xlab("Quarter after alpha assessment") + ylab("t-statistics") + ggtitle("Panel Regression t-statistics") +  theme_classic() + 
      theme(
        text = element_text(family = "Times New Roman"),
        plot.title = element_text(color = "black", size = 12, hjust = 0.5),
        axis.title = element_text(size = 12),
        legend.title = element_blank()) +
      geom_hline(aes(linetype = "*", yintercept = 1.282), color = "dark green") +
      geom_hline(aes(linetype = "**", yintercept = 1.645), color = "dark grey") +
      geom_hline(aes(linetype = "***", yintercept = 2.326), color = "grey") +
      scale_linetype_manual("sig", values = c("solid", "solid", "solid"),
                            guide = guide_legend(override.aes = list(color=c("dark green", "dark grey", "grey"))))
    

    【讨论】:

      猜你喜欢
      • 2023-01-27
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2019-06-21
      • 1970-01-01
      相关资源
      最近更新 更多