Home > Software design >  Possible to show two variables legend in same grid by ggplot (geom_line)?
Possible to show two variables legend in same grid by ggplot (geom_line)?

Time:09-22

At first, sorry for the inaccurate description because I'm not a native speaker :(

I tried really hard to use ggplot as a tool to draw images like this (also written by R in plot {graphics} function): Images wanted Cuz i want to practice ggplot so i try to draw the same one, and it turns out this is what i can do the best.... I cannot combine these exp variabla (exp1, exp2) into the same grid, if deleting the facet_grid(. ~ Exp), error orrurs: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line.

Images i can do the best

here is my code:

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"))

here is my dataset ("allDraw" in the code)

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

Hope somebody knows some methods to draw a plot like the first image by ggplot.

CodePudding user response:

Simply change the group to something like this 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)
  • Related