【问题标题】:Create the effect of drawing an additional line and legend-item on an existing ggplot2创建在现有 ggplot2 上绘制附加线和图例项的效果
【发布时间】:2017-09-16 09:47:44
【问题描述】:

我正在做一个演示,并想展示一个带有适当图例的折线图 (geom_line())。然后我想覆盖一个新的 geom_line 并添加相应的图例项。出于美学原因,我希望叠加层不要修改第一个图中给出的图例位置。效果应该是在现有图表上绘图,并添加到其图例中。

如果我只是简单地使用 ggplot 先制作第一个图,然后用两条线制作一个新图,则图例的位置会发生明显变化。

如果我尝试使第一个图成为完整图,但将其中一个线的大小设置为零,我会遇到无法抑制大小为零线的图例项的问题。

如何使用 ggplot2 达到我想要的效果?

编辑:

这是制作我第一次天真尝试的两个图表的代码。

require(ggplot2)
require(reshape2)

x<-seq(-10,10,length=200)
G <- (1/(sqrt(2*pi))) * exp(-((x)^2)/(2))
G2 <- 2*(1/(pi))*(1/(x^2+1))

df = data.frame(x,G,G2)

ggplot(data = melt(data.frame(x,G),id.vars = 'x'))+
  geom_line(aes(x=x, y=value, color=variable),size=.5)+
  scale_color_manual("Distribution",values=c("orange"),labels=c("Gaussian"))+
  coord_cartesian(ylim = c(0, 1)) 

ggplot(data = melt(data.frame(x,G,G2),id.vars = 'x'))+
  geom_line(aes(x=x, y=value, color=variable),size=.5)+
  scale_color_manual("Distribution",values=c("orange","blue"),labels=c("Gaussian","2Gaussian"))+
  coord_cartesian(ylim = c(0, 1)) 

如果从这些图片中看不出有问题,请打开这两个链接中的图片并从一个链接翻到另一个。

http://rpubs.com/jwg/269311

http://rpubs.com/jwg/269312

注意:问题比我最初描述的还要严重,因为不仅图例在移动,而且坐标轴也在移动。

大概可以通过绘制两者然后使其图例项和线不可见来解决此问题。有这种可能吗?

【问题讨论】:

  • 请提供一些示例数据、代码和输出:stackoverflow.com/questions/5963269/…
  • 你的愿望就是我的命令。如果有任何其他方法可以改进问题,请告诉我。
  • 如果您的演示格式允许嵌入动画 GIF,您可以使用 gganimate 创建一个不错的效果。如果您有兴趣,可以发布一个答案。
  • @neilfws 绝对!

标签: r ggplot2


【解决方案1】:

不确定这是否是你想要的,但这里是:

x<-seq(-10,10,length=200)
G <- (1/(sqrt(2*pi))) * exp(-((x)^2)/(2))
G2 <- 2*(1/(pi))*(1/(x^2+1))

df <- data.frame(x,G,G2)
df.plot <- tidyr::gather(df, key = 'variable', value = 'value', -x)

    ggplot(df.plot, aes(x, value, color = variable)) + geom_line() + scale_color_manual(breaks = c("G"), values = c("orange", NA)) + 
coord_cartesian(xlim = c(-10, 10), ylim = c(0,1)) + theme(legend.position = c(0,0)) + 
theme(legend.position = "right",
legend.justification = "top")

ggplot(df.plot, aes(x, value, color = variable)) + geom_line() + scale_color_manual(breaks = c("G", "G2"), values = c("orange", "blue")) + 
coord_cartesian(xlim = c(-10, 10), ylim = c(0,1)) + theme(legend.position = "right",
legend.justification = "top")

【讨论】:

    【解决方案2】:

    这里有一个解决方案,可以让一切都与动画的好处保持一致。

    library(ggplot2)
    library(tidyr)
    library(gganimate)
    
    p <- df %>% 
      gather(var, val, -x) %>% 
      ggplot(aes(x, val, frame = var)) + 
        geom_line(aes(color = var, group = var, cumulative = TRUE)) +
        coord_cartesian(ylim = c(0, 1))
    
    gganimate(p, "myplot.gif", "gif")
    

    这应该会生成一个文件myplot.gif,结果如下:

    【讨论】:

    • 我收到警告(“忽略未知美学:累积”)和错误(“不知道如何为函数类型的对象自动选择比例。默认为连续。错误( function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments 暗示不同的行数:400, 1, 0")
    • 您可以忽略的有关累积的警告(尽管有警告,它仍然有效)。我不知道另一个问题可能是什么;您给定的数据和给定的代码对我有用。我使用“最新的一切”(R 3.3.3,ggplot2 2.2.1)。
    • 我有 R 3.3.3 和 ggplot2 2.2.1。很奇怪。
    • 抱歉:我的代码中有错字。我在之前的尝试中留下了变量“dist”,它应该是“var”。已编辑,再试一次。
    • 我们起飞了。谢谢!
    猜你喜欢
    • 2023-03-07
    • 2018-08-02
    • 2020-11-09
    • 2018-02-05
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2015-06-16
    • 2015-12-15
    相关资源
    最近更新 更多