Home > other >  npdb dataset and displaying results in charts R
npdb dataset and displaying results in charts R

Time:11-22

I'm, working with the npdb dataset from UsingR package. Here is some information about it

summary of  dataset

  1. How could I calculate how much was the amount for each ear and inserting it into a pie chart?
  2. How could I determinate which states and which ID has max amount value (for each state)?
  3. How could I create a histogram with the amounts?

I was able to calculate some parts of these, but I don't know how to connect them into charts

CodePudding user response:

Did you have this in mind?

options(scipen=999)
library(UsingR)
library(dplyr)
data(npdb)
df <- npdb


q1 <- df %>% group_by(year) %>%  summarise(mean = mean(amount)) %>% rename(Ave_am = mean)
q1 <- q1 %>% 
  arrange(desc(year)) %>%
  mutate(prop = Ave_am / sum(q1$Ave_am) *100) %>%
  mutate(ypos = cumsum(prop)- 0.5*prop )

ggplot(q1, aes(x="", y=prop, fill=year))  
  geom_bar(stat="identity", width=1, color="white")  
  coord_polar("y", start=0)  
  theme_void()   
  theme(legend.position="none")  
  geom_text(aes(y = ypos, label = year), color = "white", size=6)



q2 <- df %>% group_by(state) %>% slice(which.max(amount))


ggplot(q2, aes(x=state, y=amount))   
  geom_bar(stat = "identity")  
  ylab("Max amount")

  •  Tags:  
  • r
  • Related