Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

npdb dataset and displaying results in charts R

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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")

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading