【问题标题】:Possible to show two variables legend in same grid by ggplot (geom_line)?可以通过ggplot(geom_line)在同一网格中显示两个变量图例吗?
【发布时间】:2021-11-15 01:01:02
【问题描述】:

首先,很抱歉描述不准确,因为我不是母语人士:(

我非常努力地使用 ggplot 作为工具来绘制这样的图像(也是由 R 在 plot {graphics} 函数中编写的): 因为我想练习 ggplot 所以我尝试画同一个,结果证明这是我能做的最好的......如果删除这些 exp 变量(exp1,exp2),我无法将这些facet_grid(. ~ Exp),错误或错误:geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

这是我的代码:

allDraw %>% ggplot(aes(x=PrimeType, y=mRT, shape=SOA,linetype=SOA,group=SOA,color=Exp))+
  geom_line(size=0.5,alpha=0.6) +
  scale_colour_manual(values = c('black', 'blue'))+
  scale_linetype_manual(values=c("solid", "twodash"))+
  labs(y='RT', title= 'RT on Prime Types & SOA')+
  ylim(c(600,900))+
  geom_point(size = 2)+ 
  facet_grid(. ~ Exp)+ #if delete this line the image fails to show
  geom_errorbar(aes(ymin = mRT - SE, ymax = mRT + SE), width = .1, size=0.6, alpha=0.6)

#dataset (not sure if this way of pasting data is right...)

> dput(allDraw)
structure(list(PrimeType = c("Control", "Control", "Homophone", 
"Homophone", "Semantic", "Semantic", "Control", "Control", "Homophone", 
"Homophone", "Semantic", "Semantic"), SOA = structure(c(1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("85", "150"
), class = "factor"), mRT = c(775.085416666667, 762.44, 779.744166666667, 
753.494166666667, 741.287083333333, 725.580833333333, 792.747916666667, 
798.07125, 706.307916666667, 700.527916666667, 800.543333333333, 
771.0625), N = c(24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 24L, 
24L, 24L, 24L), SE = c(20.1588447968423, 23.9092193121386, 20.4126104540678, 
22.2091770912669, 15.9753977914136, 22.6038502469004, 27.3474259511178, 
28.550485720839, 19.48170159647, 24.2353984949669, 35.3326209440763, 
25.6053095168346), Exp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor")), row.names = c(NA, 
-12L), class = c("tbl_df", "tbl", "data.frame"))

这是我的数据集(代码中的“allDraw”)

PrimeType SOA mRT N SE Exp
Control 85 775.0854166666670 24 20.158844796842300 1
Control 150 762.4400000000000 24 23.909219312138600 1
Homophone 85 779.7441666666670 24 20.412610454067800 1
Homophone 150 753.4941666666670 24 22.209177091266900 1
Semantic 85 741.2870833333330 24 15.975397791413600 1
Semantic 150 725.5808333333330 24 22.603850246900400 1
Control 85 792.7479166666670 24 27.347425951117800 2
Control 150 798.07125 24 28.55048572083900 2
Homophone 85 706.3079166666670 24 19.48170159647000 2
Homophone 150 700.5279166666670 24 24.23539849496690 2
Semantic 85 800.5433333333330 24 35.33262094407630 2
Semantic 150 771.0625 24 25.605309516834600 2

希望有人知道一些方法来绘制像ggplot 的第一张图片一样的情节。

【问题讨论】:

  • 嗨,OP - 要共享您的数据集,您能否改为执行以下操作:在控制台中键入 dput(allDraw),然后将生成的代码复制并粘贴到您的问题中,而不是您粘贴的内容中。 dput() 的输出应该以 structure(... 开头。
  • 不知道为什么表格无法显示正确的格式(预览模式下似乎正确....)谁能帮忙QQ?
  • 这就是重点 - 现在表格的格式已正确,并提供了一些编辑帮助,但如果您可以将数据作为代码提供,那么提供帮助会容易得多。使用dput() 方法复制您的数据集的所有人员只需将该代码复制并粘贴到他们自己的控制台中,因此这是共享所有内容的值和数据类型的好方法。
  • @chemdork123 谢谢!我会试试的。。给您带来的不便请见谅(我是新来的QQ)
  • @chemdork123 我这样做对吗?我粘贴了一些结果,但不确定我做得还好吗?

标签: r ggplot2 line-plot


【解决方案1】:

只需将组更改为类似paste(SOA, Exp)

allDraw %>% ggplot(aes(x=PrimeType, y=mRT, shape=SOA,linetype=SOA,group=paste(SOA, Exp),color=Exp))+
  geom_line(size=0.5,alpha=0.6) +
  scale_colour_manual(values = c('black', 'blue'))+
  scale_linetype_manual(values=c("solid", "twodash"))+
  labs(y='RT', title= 'RT on Prime Types & SOA')+
  ylim(c(600,900))+
  geom_point(size = 2)+ 
  #facet_grid(. ~ Exp)+ #if delete this line the image fails to show
  geom_errorbar(aes(ymin = mRT - SE, ymax = mRT + SE), width = .1, size=0.6, alpha=0.6)

【讨论】:

  • 哇...它真的有效!我不知道paste 语法,非常感谢!
猜你喜欢
  • 2022-01-12
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多