Search code examples
rvisualizationgraph-visualization

Bar Chart and Unstructured Data


I have simple data on reading news by "Country" so two variables. I would like to make a horizontal bar chart that would show a percentage of answers "Read" news and "Otherwise" by country. Should I transform the data first or there is a special package for bar charts with that kind of data?

My data look like that:

Country  News
UK       Read
UK       Otherwise
UK       Read
FR       Read
FR       Otherwise
FR       Otherwise
DE       Read
DE       Read
DE       Read
DK       Read
DK       Read
DK       Otherwise

Solution

  • let the original data frame be df

    library(dplyr)
    library(tidyr)
    
    df2 <- df %>% 
       group_by(Country, News) %>% 
       tally() %>% 
       complete(News, fill = list(n = 0)) %>% 
       mutate(Percentage = n / sum(n) * 100)
    
    
    ggplot(df2, aes(News, Percentage, fill = Country) + 
       geom_bar(stat = 'identity', position = 'dodge') +
       theme_bw()+coord_flip()