Adding layer with different data causes previous variable to be hidden

9 views
Skip to first unread message

Neil Jones

unread,
Jul 19, 2015, 3:45:52 AM7/19/15
to ggp...@googlegroups.com
My first attempt at a reproducible example - do hope it works.
The plot works well up to the fifth layer. When the geom_text layer is added the variable Rescale2 in the first row is not found.


I'm sure there's something simple here but I can't find it - many thanks for some help.

library("ggplot2")
# ========================================

str(grades)
str(matchedData2)

  difficulty <- ggplot(data=matchedData2, aes(x=Rescale2, y = ..density..)) + xlim(-3.5, +3.5) +
    geom_histogram(binwidth=.3,  fill="steelblue") +
    facet_grid(Isced ~ .) +
    geom_vline(aes(xintercept = value), data = grades ) +
    labs(title="Title") +
    geom_text(aes(xintercept = value, label = variable), data = grades)

  difficulty

#sessionInfo()  
#R version 3.2.0 (2015-04-16)
#Platform: x86_64-apple-darwin13.4.0 (64-bit)
#Running under: OS X 10.9.5 (Mavericks)

#locale:
#  [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

#attached base packages:
#  [1] stats     graphics  grDevices utils     datasets  methods   base     

#other attached packages:
#  [1] ggplot2_1.0.1 RSQLite_1.0.0 DBI_0.3.1    

#loaded via a namespace (and not attached):
#  [1] Rcpp_0.11.6      digest_0.6.8     MASS_7.3-40      grid_3.2.0       plyr_1.8.3       gtable_0.1.2    
#[7] magrittr_1.5     scales_0.2.5     stringi_0.4-1    reshape2_1.4.1   proto_0.3-10     tools_3.2.0     
#[13] stringr_1.0.0    munsell_0.4.2    colorspace_1.2-6

grades<-  structure(list(Language = c("E", "E", "E", "E"), Skill = c("R",  "R", "R", "R"), variable = structure(1:4, .Label = c("A1", "A2",  "B1", "B2"), class = "factor"), value = c(-0.75, 0.46, 0.92,   1.53)), row.names = c(NA, -4L), .Names = c("Language", "Skill",  "variable", "value"), class = "data.frame")
  
matchedData2<-  structure(list(Isced = c("2", "2", "2", "2", "3", "3", "3", "3","2", "2", "2", "2", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "2", "2", "2", "2", "3", "3", "3", "3", "2", "2","2", "2", "2", "2", "2", "2", "3", "3", "3", "3", "3", "3", "3", "3", "2", "2"), TrueScore = c(-4.03, -7.54, -0.53, 2.04, 1.97,2.01, 1, 1.23, 0.64, 0.24, 0.17, -0.02, 1.05, 0.27, 0.55, 0.41, 0.46, 0.33, 0.54, 0.43, 0.11, 0.01, 1.09, 2.18, -0.41, 0.24, 0.17, -0.34, 2.93, 4.11, 3.33, 4.4, -0.25, -1.77, -0.1, -0.36,  -1.78, -1.29, -0.92, -1.28, 2.04, 1.47, 1.21, 1.72, -0.53, -0.58,1.82, 3.38, -6.07, -8.1), Rescale1 = c(-4.03, -10.6766924607636, -0.53, 2.69220696634228, 1.97, 2.65034193682107, 1, 1.56185116926966,  0.64, 0.180305195069781, 0.17, -0.182525060780691, 1.05, 0.22217022459099,  0.55, 0.417540362356629, 0.46, 0.305900283633406, 0.54, 0.445450382037434,  0.11, -0.140660031259483, 1.09, 2.88757710410792, -0.41, 0.180305195069781,  0.17, -0.629085375673581, 2.93, 5.58089400330566, 3.33, 5.98558928867734,   -0.25, -2.62465178285118, -0.1, -0.656995395354386, -1.78, -1.95481131051185,  -0.92, -1.94085630067144, 2.04, 1.89677140543932, 1.21, 2.24564665144939, -0.53, -0.964005611843248, 1.82, 4.56217828495626, -6.07, -11.4581730118261 ), Rescale2 = c(-3.35024526597652, -10.1028959748899, 0.205549984174354,  3.47912376300948, 2.74540373428212, 3.43659134211835, 1.75994047924031,  2.33074839894909, 1.39420153922479, 0.927178509541958, 0.916709034204528,   0.558564195152205, 1.81073755424246, 0.969710930433084, 1.30276680422091,  1.16819556125834, 1.21133206921703, 1.05477577221533, 1.29260738922048,  1.19655050851909, 0.855752544201942, 0.601096616043331, 1.85137521424419,     3.67760839383473, 0.327462964179527, 0.927178509541958, 0.916709034204528,    0.104885038980202, 3.7207075743235, 6.41386080449713, 4.12708417434074,    6.82500753977801, 0.490013604186424, -1.92249369016344, 0.642404829192889,    0.0765300917194516, -1.06437689087953, -1.24197495590543, -0.190667200842457,  -1.22779748227506, 2.81651963928514, 2.6710077660781, 1.97328819424936,   3.02544460683747, 0.205549984174354, -0.235374328148801, 2.59301250927565,   5.37890522947974, -5.42276592606445, -10.8968344981909)), .Names = c("Isced",   "TrueScore", "Rescale1", "Rescale2"), row.names = c(NA, 50L), class = "data.frame")


Ron Crump

unread,
Jul 19, 2015, 4:00:06 AM7/19/15
to Neil Jones, ggp...@googlegroups.com


Hi Neil,

Sorry if this answer is not very detailed, I'm sat on my couch with my phone so can't play with your example and test a solution. But thanks for providing all the info.

>I'm sure there's something simple here but I can't find it - many
>thanks
>for some help.

Your call to geom_text has xintercept and label in the aesthetics, it should have x, y and label.

Hope this helps.

Ron.

Dennis Murphy

unread,
Jul 19, 2015, 1:41:38 PM7/19/15
to Neil Jones, ggplot2
Ron is right about using x as an aesthetic to be mapped in
geom_text(), but you have another problem: ..density.. is returned by
stat_bin() and passed into geom_histogram() to produce the histogram,
but that information is not available to any other layer in your
graph. If you try to use ..density.. implicitly by mapping it to y
globally in ggplot() or explicitly try to map it to the y aesthetic
locally in geom_text(), ggplot2 will return an error. Perhaps a fixed
y-value is desired for the labels, in which case the ggplot() call
could look something like

ggplot(data=matchedData2) +
xlim(-3.5, +3.5) + ylim(0, 1) +
geom_histogram(aes(x = Rescale2, y = ..density..),
binwidth=.3, fill="steelblue") +
facet_grid(Isced ~ .) +
geom_vline(aes(xintercept = value), data = grades ) +
labs(title="Title") +
geom_text(aes(x = value, label = variable), y = 0.92, data = grades)

Notice how I moved the aesthetic mappings from ggplot() to
geom_histogram() since you are using multiple data frames to produce
the graph and the names of the variables mapped to x and y change in
each layer.

I have the sense this is not exactly what you want; if so, you might
want to be more explicit about how you want this graph to look.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages