开发者

R ggplot2 facetting - Error: No Layers in Plot

I've been struggling with for a couple of hours but I'm sure it's dead simple.

I have a dataset that you can reproduce by copying and pasting from the jumble at the bottom here.

It starts off looking like this

> head(mydata)
  POSITION W_MEAN T_MEAN W_STDEV T_STDEV COUNT POSCAT
1        1 108.36 109.37    5.02    4.61   117  START
2        2 107.31 109.32    4.50    3.67   167  START
3        3 108.82 109.72    4.62    4.70   162  START
4        4 109.73 111.17    3.90    3.29   154  START
5        5 109.69 111.16    4.31    4.41   163  START
6        6 110.23 111.69    4.71    3.68   159  START

POSCAT is category I've assigned according to position. 1-40 = START, 41-120 = MIDDLE and 121+ = END.

I've produced a nice histogram for the whole dataframe using

m <- ggplot(mydata, aes(x=T_MEAN))
m + geom_histogram(aes(y = ..density..)) + geom_density()

But I want to facet the histogram on START, MIDDLE and END using POSCAT, so I've tried this

m <- ggplot(mydata, aes(x=T_MEAN))
m + geom_histogram(aes(y = ..density..)) + geom_density()
m + facet_grid(~ POSCAT) 

And it gives me this error

Error: No Layers in Plot

What am I doing wrong?

Thanks for the help!

mydata <- structure(list(POSITION = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 11开发者_运维技巧6, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163), W_MEAN = c(108.36, 107.31, 108.82, 109.73, 109.69, 110.23, 109.64, 109.69, 110.81, 109.89, 110.41, 110.34, 110.68, 110.39, 111.18, 110.59, 110.69, 110.5, 111.5, 111.33, 111.05, 111.78, 111.59, 111.94, 111.4, 112.09, 111.74, 112.23, 112.08, 112.68, 112.73, 112.72, 112.11, 112.36, 112.25, 112.65, 112.57, 112.86, 112.33, 112.74, 113.29, 112.47, 112.78, 112.18, 112.24, 112.8, 112.92, 112.36, 112.88, 112.86, 112.78, 113.14, 112.97, 112.84, 112.41, 112.94, 112.52, 112.63, 113.19, 112.71, 112.89, 112.83, 113.15, 112.51, 112.81, 112.72, 112.2, 113.04, 111.49, 113.06, 112.48, 112.82, 112, 112.62, 112.24, 112.8, 112.41, 112.67, 112.75, 111.21, 112.52, 111.88, 112.58, 111.08, 112.58, 112.67, 112.15, 113.01, 111.97, 112.32, 112.06, 112.52, 112.11, 112.38, 111.57, 112.33, 112.03, 111.97, 111.99, 111.94, 112.29, 111.43, 111.72, 111.73, 112.08, 111.45, 111.56, 111.79, 111.07, 111.35, 111.29, 111.35, 110.93, 110.87, 110.64, 110.74, 110.52, 110.39, 110.14, 109.91, 110.95, 110.85, 111.08, 110.49, 110.81, 109.8, 110.34, 110.14, 109.95, 110.46, 110.5, 110.53, 110.74, 110.39, 109.5, 110.28, 110.46, 110.57, 110.22, 110.42, 110.2, 110.16, 110.04, 110.52, 110.79, 109.43, 110.55, 110.35, 110.66, 110.05, 110.73, 110.48, 110.73, 109.8, 110.95, 110.33, 110.26, 110.7, 110.5, 110.28, 110.39, 110.73, 109.96), T_MEAN = c(109.37, 109.32, 109.72, 111.17, 111.16, 111.69, 111.6, 111.59, 112.07, 111.78, 112.01, 112.16, 112.43, 112.23, 112.17, 112.6, 112.48, 112.45, 113.4, 113.02, 113.08, 113.2, 113.41, 113.38, 113.41, 113.64, 113.5, 114.1, 113.97, 114.22, 114.42, 114.37, 114.06, 114.07, 114.06, 114.25, 114.18, 114.57, 114.4, 114.25, 114.97, 114.4, 114.64, 114.29, 114.3, 114.5, 114.5, 114.45, 114.48, 114.89, 114.46, 114.77, 114.76, 114.3, 114.47, 114.4, 114.61, 114.25, 114.5, 114.73, 114.73, 114.42, 114.34, 114.52, 114.39, 114.43, 114.02, 114.23, 113.8, 114.4, 114.17, 114.35, 114.03, 114.29, 114.44, 114.19, 114.27, 114.22, 114.25, 113.4, 113.84, 113.99, 113.82, 113.32, 113.93, 114.26, 114.04, 114.4, 114.06, 113.96, 113.97, 114.05, 113.72, 113.94, 113.51, 113.97, 113.64, 113.54, 113.57, 113.78, 113.59, 113.01, 113.5, 113.43, 113.44, 113.02, 113.4, 113.36, 112.97, 112.65, 112.95, 112.99, 112.51, 112.45, 112.26, 112.51, 112.09, 111.86, 111.8, 111.68, 112.46, 112.33, 112.67, 112.02, 112.36, 111.46, 111.88, 111.76, 111.28, 111.97, 112.05, 112.1, 112.25, 111.69, 111.28, 111.87, 111.85, 111.98, 111.77, 111.81, 111.78, 111.72, 111.47, 112.01, 112.22, 110.95, 112.06, 111.87, 112.02, 111.63, 111.95, 112.08, 112, 111.48, 112.11, 111.5, 111.85, 112.03, 111.87, 111.53, 111.8, 111.73, 111.44), W_STDEV = c(5.02, 4.5, 4.62, 3.9, 4.31, 4.71, 3.89, 4.59, 4.24, 4.08, 4.19, 3.66, 3.66, 3.93, 3.79, 3.62, 3.67, 3.74, 3.4, 3.74, 3.33, 3.34, 2.98, 3.69, 3.55, 3.18, 3.12, 3.28, 3.58, 3.57, 3.81, 3.14, 3.45, 3.59, 3.81, 3.82, 3.22, 3.37, 3, 3.09, 3.07, 2.96, 2.86, 2.83, 2.72, 2.91, 2.77, 3.17, 3.57, 3.11, 3.2, 3.14, 3.31, 3.71, 3.9, 3.64, 3.21, 2.99, 3.39, 2.99, 3.39, 3.41, 3.12, 3.39, 3.09, 3.16, 3.22, 2.79, 3.13, 3.27, 4.09, 3.02, 3.15, 2.98, 3.13, 3.3, 3.07, 3.07, 3.26, 3.15, 3.35, 3.23, 3.47, 3.65, 2.79, 2.78, 3.3, 3.08, 2.91, 2.76, 2.91, 3.05, 3.24, 3.28, 2.84, 2.76, 2.72, 2.97, 3.44, 2.75, 3.16, 3.58, 2.99, 3.11, 2.96, 3.74, 2.89, 3.51, 3.54, 3.75, 3.36, 3.73, 3.34, 3.64, 3.81, 3.27, 3.87, 3.62, 3.8, 3.36, 3.25, 3.41, 3.33, 3.52, 3.57, 3.96, 3.75, 3.78, 3.57, 3.28, 3.14, 3.53, 3.26, 3.38, 4.39, 3.13, 3.18, 3.13, 3.61, 3.72, 3.47, 3.52, 3.77, 3.26, 3.55, 3.98, 3.63, 3.54, 3.47, 3.42, 3.33, 3.73, 3.04, 3.51, 3.04, 3.63, 2.98, 3.22, 3.47, 3.62, 3.74, 2.9, 4.18), T_STDEV = c(4.61, 3.67, 4.7, 3.29, 4.41, 3.68, 3.19, 3.56, 3.19, 3.43, 3.14, 2.97, 2.83, 3.34, 3.51, 2.78, 2.65, 2.56, 2.75, 2.84, 2.52, 2.66, 2.56, 2.56, 2.47, 2.39, 2.61, 2.44, 2.62, 2.4, 2.46, 2.28, 2.39, 2.5, 2.66, 2.49, 2.33, 2.42, 2.4, 2.49, 2.38, 2.28, 2.32, 2.36, 2.39, 2.13, 2.18, 2.56, 2.44, 2.23, 2.48, 2.41, 2.19, 2.59, 2.44, 2.58, 2.49, 2.28, 2.37, 2.35, 2.3, 2.47, 2.25, 2.71, 2.33, 2.42, 2.58, 2.14, 2.4, 2.48, 3.08, 2.33, 2.33, 2.36, 2.33, 2.53, 2.51, 2.62, 2.6, 2.45, 2.51, 2.55, 2.67, 2.81, 2.32, 2.2, 2.59, 2.53, 2.28, 2.27, 2.15, 2.49, 2.44, 2.41, 2.49, 2.35, 2.37, 2.57, 2.85, 2.4, 2.77, 2.98, 2.45, 2.67, 2.56, 3.15, 2.74, 2.87, 2.96, 3.41, 3.04, 3.25, 3.02, 3.49, 3.42, 2.97, 3.66, 3.46, 3.62, 3.22, 3.16, 3.41, 3.26, 3.35, 3.34, 3.79, 3.65, 3.53, 3.09, 2.95, 3.1, 3.2, 3.04, 3.33, 4.14, 3.01, 2.92, 3.07, 3.35, 3.75, 3.31, 3.27, 3.62, 3.28, 3.31, 4.18, 3.74, 3.6, 3.4, 3.34, 3.23, 3.58, 3.02, 3.27, 2.97, 3.68, 2.92, 3.31, 3.36, 3.52, 3.69, 3.51, 4.27), COUNT = c(117, 167, 162, 154, 163, 159, 164, 170, 171, 170, 168, 170, 163, 166, 172, 173, 173, 166, 173, 163, 177, 174, 175, 173, 175, 170, 165, 172, 175, 176, 174, 175, 174, 168, 174, 171, 174, 175, 176, 170, 171, 168, 171, 165, 171, 170, 170, 174, 173, 174, 158, 170, 168, 170, 168, 169, 174, 171, 166, 168, 169, 172, 158, 163, 173, 167, 172, 167, 169, 168, 166, 165, 171, 158, 158, 170, 174, 173, 169, 164, 168, 174, 168, 169, 170, 174, 174, 171, 159, 161, 169, 163, 169, 169, 164, 172, 171, 164, 170, 165, 161, 162, 165, 163, 166, 169, 173, 168, 169, 165, 169, 166, 163, 170, 171, 172, 169, 169, 166, 163, 168, 166, 168, 168, 172, 171, 172, 168, 172, 164, 169, 169, 170, 172, 171, 167, 161, 166, 170, 170, 172, 169, 173, 160, 168, 161, 171, 173, 171, 166, 158, 170, 167, 166, 169, 169, 159, 160, 157, 150, 159, 146, 88), POSCAT = c("START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "START", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "MIDDLE", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END", "END")), .Names = c("POSITION", "W_MEAN", "T_MEAN", "W_STDEV", "T_STDEV", "COUNT", "POSCAT"), row.names = c(NA, 163L), class = "data.frame")


You had everything there, just an error in your assignment to m. Either of these should do the trick:

m <- ggplot(mydata, aes(x=T_MEAN))
m + geom_histogram(aes(y = ..density..)) + geom_density() + facet_grid(~ POSCAT) 

or

m <- ggplot(mydata, aes(x=T_MEAN))
m <- m + geom_histogram(aes(y = ..density..)) + geom_density() 
m + facet_grid(~ POSCAT) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜