如何在 X 轴上显示具有多个值的 ggplot?
How can I display a ggplot with multiple values on the X axis?
基本上这就是我想要的:
但是我的数据是这种格式:
Country
Response
Spain
No
France
Yes
France
No
France
Yes
UK
Yes
并且基本上想要对每个国家/地区的“否”和“是”答复求和
给你:
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
我添加了一些额外的行,以便您可以使用它。
基本上这就是我想要的:
但是我的数据是这种格式:
Country | Response |
---|---|
Spain | No |
France | Yes |
France | No |
France | Yes |
UK | Yes |
并且基本上想要对每个国家/地区的“否”和“是”答复求和
给你:
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
我添加了一些额外的行,以便您可以使用它。