SHARE
TWEET

Untitled

a guest Jun 26th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cat <- data.frame(Breed = c("American Shorthair","Ragdoll","Persian","Sphynx","Maine Coon"),
  2. Longevity = c("17","19","15","17","20"))
  3.  
  4. dog <- data.frame(Breed = c("Havanese","Bulldog","Beagle","Chihuahua","Poodle"),
  5. Longevity = c("16","11","12","15","24"))
  6.  
  7. #etc for types of birds, fish, snakes, and ferrets
  8.  
  9. #the following works
  10. plot <- ggplot(data = cat, aes(x = Breed, y = Longevity, fill = Breed)) +
  11.   geom_bar(stat = "identity", position = position_dodge()) +
  12.   xlab("Breed") +
  13.   ylab("Longevity") +
  14.   ggtitle("cat") +
  15.   geom_text(aes(label = Longevity), vjust = -0.3, color = "black", size = 3.5) +
  16.   theme(axis.line = element_line(color = "black"), axis.text = element_text(color = "black"),
  17.         legend.position = "none", plot.title = element_text(hjust = .5),
  18.         panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
  19.         panel.border = element_rect(color = "black", fill = NA, size = 0.8),
  20.         panel.background = element_rect(fill = NA), text = element_text(size=10))
  21. print(plot)
  22.  
  23. gg_pets <- c("cat","dog","bird","fish","snake","ferret")
  24.  
  25. #the following does not work
  26. for (i in 1:length(gg_pets)){
  27.     plot <- ggplot(data = [i], aes(x = Breed, y = Longevity, fill = Breed)) +
  28.     geom_bar(stat = "identity", position = position_dodge()) +
  29.     xlab("Breed") +
  30.     ylab("Longevity") +
  31.     ggtitle([i]) +
  32.     geom_text(aes(label = Longevity), vjust = -0.3, color = "black", size = 3.5) +
  33.     theme(axis.line = element_line(color = "black"), axis.text = element_text(color = "black"),
  34.           legend.position = "none", plot.title = element_text(hjust = .5),
  35.           panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
  36.           panel.border = element_rect(color = "black", fill = NA, size = 0.8),
  37.           panel.background = element_rect(fill = NA), text = element_text(size=10))
  38.     print(plot)
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top