Home > Software engineering >  How can I display a ggplot with multiple values on the X axis?
How can I display a ggplot with multiple values on the X axis?

Time:05-10

Basically this is what I'm going for:

Chart example

My data however is in this format:

Country Response
Spain No
France Yes
France No
France Yes
UK Yes

and basically would want to sum the "No" and "Yes" responses for each countryy

CodePudding user response:

Here you go:

library(tidyverse)

# Generate some random data to work with
set.seed(123) 
Country <- rep(c("Spain", "France", "UK"), times = c(200,200,200))
Response <-  sample(c("Yes", "No"),600, replace = T, prob = c(.3,.6))
df <- data.frame(Country, Response)

# Barplot
p <- df %>%  ggplot(aes(x = Country, fill = Response))  
  geom_bar(position=position_dodge())  
  ggtitle("Response of each country")  
  xlab("")  
  ylab("")  
  theme_bw()  
  theme(plot.title = element_text(size = 12, face = "bold", hjust = 0.5))
  
p

I have added a few extra lines so you can play around with it.

  • Related