Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

* Plot is missing `lg ` Error in ggplot graph-making

$
0
0

I have been copying the code from: Chang, 2012, R Graphics Cookbook: Practical Recipes for Visualizing Data. pg, 46-47

Which is this:

ggplot(prac1, aes(x=pos1, y=pos2)) +
  geom_segment(aes(yend=pos2), xend=0, colour="grey50") +
  geom_point(size=3, aes(colour=lg)) +
  scale_colour_brewer(palette="Set1", limits=c("NL","AL")) +
  theme_bw() +
  theme(panel.grid.major.y = element_blank(), # No horizontal grid lines
        legend.position=c(1, 0.55), # Put legend inside plot area
        legend.justification=c(1, 0.5))

OR

ggplot(prac1, aes(x=pos1, y=pos2)) +
  geom_segment(aes(yend=pos2), xend=0, colour="grey50") +
  geom_point(size=3, aes(colour=lg)) +
scale_colour_brewer(palette="Set1", limits=c("NL","AL"), guide=FALSE) +
  theme_bw() +
  theme(panel.grid.major.y = element_blank()) +
  facet_grid(lg ~ ., scales="free_y", space="free")

One of the two errors are returned: (1) Error: At least one layer must contain all faceting variables: `lg`. * Plot is missing `lg` * Layer 1 is missing `lg` * Layer 2 is missing `lg

(2) Error in FUN(X[[i]], ...) : object 'lg' not found

My data structure:

str(prac1)
'data.frame':   32 obs. of  3 variables:
 $ Common.Name   : Factor w/ 32 levels "Blackbird","Blue Tit ",..: 8 1 23 3 7 28 12 31 25 17 ...
 $ G.vs.S        : Factor w/ 2 levels "G","S": 1 1 1 1 2 2 2 1 2 2 ...
 $ Average.weight: num  21.2 101.8 75 22.5 47 ...

REPRODUCIBLE CODE:

prac1 <- dstructure(list(Common.Name = structure(c(8L, 1L, 23L, 3L, 7L, 
28L, 12L, 31L, 25L, 17L, 32L, 22L, 15L, 19L, 6L, 11L, 21L, 26L, 
9L, 29L, 14L, 13L, 27L, 16L, 30L, 20L, 2L, 10L, 18L, 5L, 4L, 
24L), .Label = c("Blackbird", "Blue Tit ", "Bullfinch", "Buzzard", 
"Chaffinch ", "Common Whitethroat", "Corn Bunting", "Dunnock", 
"Goldfinch", "Great tit ", "Greenfinch", "Grey Partridge", "House martin", 
"Jackdaw", "Kestrel", "Lapwing ", "Linnet", "Long-tailed Tit ", 
"Reed Bunting", "Robin ", "Rook", "Skylark", "Song Thrush", "Sparrowhawk", 
"Starling", "Stock dove", "Swallow", "Turtle Dove", "Woodpigeon", 
"Wren ", "Yellow Wagtail", "Yellowhammer"), class = "factor"), 
    G.vs.S = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 
    2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("G", "S"), class = "factor"), 
    Average.weight = c(21.17, 101.8, 74.95, 22.51, 47, 156.8, 
    390, 17.55, 84.99, 18.79, 25.89, 38.56, 205.5, 19.18, 13.76, 
    27.66, 452.3, 326.3, 15.8, 507.4, 231.8, 17.81, 19.9, 250.3, 
    9.91, 18.98, 10.88, 18.61, 7.78, 21.81, 828.2, 205)), row.names = c(NA, 
-32L), class = "data.frame")

My X and Y variables:

pos1 <- prac1$Common.Name
pos2 <- prac1$Average.weight

How can I fix the error, the end result should have a graph looking like this but with my data: enter image description here


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>