ggplot2 - R ggplot multivariable facet -


i have df usarrests, contains 3 crime statistics per state , additional urbanpop variable. i'm trying create faceted bar chart(s) contain 3 crime statistics of each state, y-axis being cases per 100,000 people @ specific breaks (100, 200, 300). top of graph read state from.

my main issue i'm not understanding mapping/aes function , how relates pulling 3 variables in 1 graph. thanks

library(ggplot2)  usarrests$state_name <- rownames(usarrests)  p <- ggplot() +   layer(data = usarrests,         stat = "identity",         geom = "bar",         mapping = aes(x = assault, rape, murder),           position = "identity") +   scale_y_discrete(name = "cases per 100,000 people",                      breaks = c(100, 200, 300),                      labels = c("100", "200", "300")) +            facet_wrap(~ state_name) plot(p) 

you need reshape data frame. usarrests2 reshaped data frame. after that, can plot crime category on x axis , number continuous on y-axis. in case, can use geom_col() instead of geom_bar(stat = "identity"). same. facet_wrap way generate facets. can use ncol or nrow specify column or row numbers.

library(dplyr) library(tidyr) library(ggplot2)  usarrests2 <- usarrests %>%   gather(crime, number, assault, rape, murder)  ggplot(usarrests2, aes(x = crime, y = number)) +   geom_bar(stat = "identity") +   scale_y_continuous(breaks = c(100, 200, 300)) +   facet_wrap(~ state_name, nrow = 5)  

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -