简体   繁体   中英

Issues with displaying data points on every frame of facet_wrap/facet_grid object

I'm trying to produce a plot with either facet_wrap or facet_grid (no preference at this time), but display a selection of data points on every frame within the facet_wrap/facet_grid object.

I read that you can simply remove the facetting variable from the data set you want included on every plot, but for whatever reason this doesn't seem to be working for me.

This is on Rstudio Version 1.1.453.

I found this code sample:

ggplot(mpg, aes(displ, hwy)) +
  geom_point(data = transform(mpg, class = NULL), colour = "grey85") +
  geom_point() +
  facet_wrap(~class)

And pretty much copied it for my code below. The above code works fine, but for whatever reason in my implementation it returns an error message. Note I've tried setting both geom features to geom_point also with no luck.

ggplot(data = Total, aes(Total$Time, Total$Killing)) + 
  geom_jitter(data = transform(Total, Run = NULL), colour = "grey85") +
  geom_point() + 
  facet_wrap(~Run)

Error: Aesthetics must be either length 1 or the same as the data (2700): x, y

This is the error message I've been encountering on attempting to run this code.

Ultimately my goal is to run the below code, but I simplified it a bit for the purposes of the question above.

ggplot(data = filter(Total, Cell_Line != "stDev"), aes(x= Time, y=Killing)) + 

  geom_line(data = filter(select(Total, -Run), Cell_Line == "Wild_Type"), aes(x = Time, y = filter(Total, Cell_Line == "Wild_Type")[,3])) +

  geom_errorbar(aes(x = filter(Total, Cell_Line == "Wild_Type")[,2], ymax = filter(Total, Cell_Line == "Wild_Type")[,3] + filter(Total, Cell_Line == "stDev")[,3], ymin = filter(Total, Cell_Line == "Wild_Type")[,3] - filter(Total, Cell_Line == "stDev")[,3])) +

  geom_point() + 

  facet_wrap(~Run)

And here's the result of dput(Total) trimmed down to the first 30 rows:

structure(list(Cell_Line = structure(c(5L, 12L, 13L, 1L, 2L, 
3L, 4L, 6L, 7L, 8L, 9L, 10L, 11L, 15L, 14L, 5L, 12L, 13L, 1L, 
2L, 3L, 4L, 6L, 7L, 8L, 9L, 10L, 11L, 15L, 14L), .Label = c("17", 
"19", "20", "29", "3", "33", "38", "47", "49", "53", "55", "7", 
"8", "stDev", "Wild_Type"), class = "factor"), Time = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("00", 
"02", "04", "08", "12", "18", "24", "32", "40", "48", "56", "64", 
"72", "80"), class = "factor"), Killing = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0704388, 0.2881066, -0.0132908, 
0.04700991, 0.03049371, -0.02243472, 0.1513817, 0.129636, 0.09328508, 
0.05876777, 0.1063291, 0.0357473, 0.1974026, 0.07732854, 0.07383331
)), row.names = c(NA, 30L), class = "data.frame")

Your call to transform has an error: you don't have a column named Run .

set.seed(1)
Total$Run <- sample(1:100, 30)

# this is your own code:
ggplot(data = Total, aes(Total$Time, Total$Killing)) + 
  geom_jitter(data = transform(Total, Run = NULL), colour = "grey85") +
  geom_point() + 
  facet_wrap(~Run)

Which produces this plot: 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM