【问题标题】:ggplot2: How to create dashed lines in legend?ggplot2:如何在图例中创建虚线?
【发布时间】:2017-12-01 14:41:44
【问题描述】:

我正在尝试创建一个由 2 个直线密度和 2 个虚线密度组成的 ggplot。 2 个密度为蓝色,另外 2 个为红色。我的情节应该包括一个关于这些密度的颜色和线条的图例。

尽管我认为必须有一个简单的解决方案,但我无法在我的图例中生成虚线。我试图根据几个线程解决我的问题,例如here 或 here,但我的 ggplot 代码已经有点复杂,这些线程的示例以不同的方式使用 ggplot。因此,我无法重现这些线程的结果。

这是我迄今为止所做的一个可重复的示例。最后一行 scale_linetype_manual 似乎不起作用。如何在我的图例中为 grp3 和 grp4 生成虚线?

library(ggplot2)

set.seed(123)

# Example data
N <- 1000
x1 <- rnorm(N, 5)
x2 <- rnorm(N, 10)
x3 <- rnorm(N, 15)
x4 <- rnorm(N, 20)

ggplot() + 
  stat_density(aes(x = x1, col = "grp1"), position = "identity", geom = "line") + 
  stat_density(aes(x = x2, col = "grp2"), position = "identity", geom = "line") +
  stat_density(aes(x = x3, col = "grp3"), position = "identity", geom = "line", linetype = 2) +
  stat_density(aes(x = x4, col = "grp4"), position = "identity", geom = "line", linetype = 2) +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 90, hjust = 0.3), 
        axis.title.x = element_text(vjust = - 0.5), 
        plot.title = element_text(vjust = 1.5), 
        legend.title = element_blank(), legend.position = c(0.75, 0.85), 
        legend.key = element_blank(), legend.text = element_text(size = 10)) + 
  scale_color_manual(values = c("red", "dodgerblue3", "red", "dodgerblue3")) + 
  scale_linetype_manual(values = c("grp1" = 1, "grp2" = 1, "grp3" = 2, "grp4" = 2))

【问题讨论】:

  • 从您的第一个链接:“将linetype = 放入aes()”
  • @Henrik 如果我将linetype = 2 放入aes,它会返回错误Error: Continuous value supplied to discrete scale。如果我输入 linetype = "grp3" 它会创建 2 个不同的图例。

标签: r ggplot2 legend


【解决方案1】:
library(reshape)
library(ggplot2)
N <- 1000
x1 <- rnorm(N, 5);  x2 <- rnorm(N, 10);  x3 <- rnorm(N, 15);  x4 <- rnorm(N, 20)

df1 <- data.frame(x1,x2,x3,x4)
df2 <- melt(df1)

ggplot(data=df2,aes(x=value, col=variable, linetype=variable)) +
 stat_density(position = "identity", geom = "line") +
  theme_bw() +
  theme(axis.text.y = element_text(angle = 90, hjust = 0.3), 
        axis.title.x = element_text(vjust = - 0.5), 
        plot.title = element_text(vjust = 1.5), 
        legend.title = element_blank(), 
        legend.key = element_blank(), legend.text = element_text(size = 10)) +
  scale_color_manual(values = c("red", "dodgerblue3", "red", "dodgerblue3")) + 
  scale_linetype_manual(values = c(1, 1, 2, 2)) +
  theme(legend.key.size =  unit(0.5, "in"))

【讨论】:

    【解决方案2】:

    如果将其转换为具有分组列的数据框,则可以使用this answer 中的方法进行此操作。

    代码如下:

    library(ggplot2)
    library(dplyr)
    
    set.seed(123)
    
    # Example data
    N <- 1000
    x1 <- rnorm(N, 5)
    x2 <- rnorm(N, 10)
    x3 <- rnorm(N, 15)
    x4 <- rnorm(N, 20)
    
    df <- data.frame(x = c(x1, x2, x3, x4), 
                     group = rep(c("grp1", "grp2", "grp3", "grp4"), each = N))
    
    df %>%
      ggplot(aes(x, color = group)) +
      geom_density(aes(linetype = group)) +
      scale_color_manual(name = "Treatment & State",
                          labels = c("grp1", "grp2", "grp3", "grp4"),
                          values = c("red", "dodgerblue3", "red", "dodgerblue3")) +   
      scale_linetype_manual(name = "Treatment & State",
                         labels = c("grp1", "grp2", "grp3", "grp4"),
                         values = c("grp1" = 1, "grp2" = 1, "grp3" = 2, "grp4" = 2))
    

    给你这张图片

    【讨论】:

    • 感谢您的出色解决方案!效果很好。
    猜你喜欢
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多