将条从低到高重新排序
Reordering the bars from low to high
我正在尝试制作条形图
ggplot(data= Dataa, aes(x = Gender, fill = Q1)) +
theme_bw() +
geom_bar(position = "dodge") +
labs(title = "Figure 1.1", subtitle = "Distribution of national exam scores by region", y = "Count", x = "Average score for each student (MSTUDENT)") +
theme(plot.subtitle=element_text(face="bold")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(plot.title=element_text(face="bold"))
我想为男女订购酒吧,升序,
实现您想要的结果的一个选项:
- 不是通过
geom_bar
计算计数,而是在将数据传递给 ggplot
之前汇总您的数据
- 按
Gender
排列数据并计数(默认情况下按 dplyr::count
命名为 n
- 按
Gender
分组并添加一个助手 id
或更准确地说是每个 Q1
类别的排名。
- 在
group
美学上映射辅助列,以便根据等级对条形进行排序。
此外,我们必须切换到 geom_col
并将手动计算的计数映射到 y
美学上。
使用一些伪造的随机示例数据:
library(ggplot2)
library(dplyr)
# Aggregate data, arrange and add helper variable
Dataa1 <- Dataa %>%
count(Gender, Q1) %>%
arrange(Gender, n) %>%
group_by(Gender) %>%
mutate(id = row_number())
ggplot(data= Dataa1, aes(x = Gender, y = n, group = id, fill = Q1)) +
theme_bw() +
geom_col(position = "dodge") +
labs(title = "Figure 1.1", subtitle = "Distribution of national exam scores by region", y = "Count", x = "Average score for each student (MSTUDENT)") +
theme(plot.subtitle=element_text(face="bold")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(plot.title=element_text(face="bold"))
数据
set.seed(123)
Dataa <- data.frame(
Gender = rep(c("Kvinner", "Mann"), 100),
Q1 = sample(paste0("Bank", 1:10), 200, replace = TRUE)
)
Dataa$Q1 <- factor(Dataa$Q1, levels = paste0("Bank", 1:10))
我正在尝试制作条形图
ggplot(data= Dataa, aes(x = Gender, fill = Q1)) +
theme_bw() +
geom_bar(position = "dodge") +
labs(title = "Figure 1.1", subtitle = "Distribution of national exam scores by region", y = "Count", x = "Average score for each student (MSTUDENT)") +
theme(plot.subtitle=element_text(face="bold")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(plot.title=element_text(face="bold"))
我想为男女订购酒吧,升序,
实现您想要的结果的一个选项:
- 不是通过
geom_bar
计算计数,而是在将数据传递给ggplot
之前汇总您的数据
- 按
Gender
排列数据并计数(默认情况下按dplyr::count
命名为 - 按
Gender
分组并添加一个助手id
或更准确地说是每个Q1
类别的排名。 - 在
group
美学上映射辅助列,以便根据等级对条形进行排序。
n
此外,我们必须切换到 geom_col
并将手动计算的计数映射到 y
美学上。
使用一些伪造的随机示例数据:
library(ggplot2)
library(dplyr)
# Aggregate data, arrange and add helper variable
Dataa1 <- Dataa %>%
count(Gender, Q1) %>%
arrange(Gender, n) %>%
group_by(Gender) %>%
mutate(id = row_number())
ggplot(data= Dataa1, aes(x = Gender, y = n, group = id, fill = Q1)) +
theme_bw() +
geom_col(position = "dodge") +
labs(title = "Figure 1.1", subtitle = "Distribution of national exam scores by region", y = "Count", x = "Average score for each student (MSTUDENT)") +
theme(plot.subtitle=element_text(face="bold")) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"))+
theme(plot.title=element_text(face="bold"))
数据
set.seed(123)
Dataa <- data.frame(
Gender = rep(c("Kvinner", "Mann"), 100),
Q1 = sample(paste0("Bank", 1:10), 200, replace = TRUE)
)
Dataa$Q1 <- factor(Dataa$Q1, levels = paste0("Bank", 1:10))