仅使用一个变量(w/o 值或排名)对 ggplot2 条形图中的条重新排序?
Reordering bars in ggplot2 bar graph using only one variable (w/o value or ranking)?
我想重新排列我的 ggplot 条形图的条形 - 在 Whosebug 上有很多类似的条目(例如 here)。
但是,我的问题是:您能否通过告诉 ggplot 不要按标签的字母顺序排序而是按相同标签的计数作为感兴趣的价值。
就我而言,我有关于哪个政党拥护某个问题/在给定问题领域最有能力的问题的调查数据。
respondent-id competence
1 "Party A"
2 "Party A"
3 "Party B"
4 "Party B"
5 "Party B"
6 "Party C"
ggplot 现在要做的是一个条形图,其中有第二高的第一个(甲方),第二高的(乙方)和最后一个最低的(丙方)。但是我如何告诉 ggplot 将计数考虑在内(2:3:1 --> 将 B 方放在第一位)?
我按照建议 here 尝试了几种方法,但这并没有解决问题:大多数方法都包含一个位置变量,它会告诉 ggplot "assign party B first place"。我还尝试通过 "competence" 来 reorder()
,但没有成功。最后,我可以为各方分配不同的前缀(“1_party_B”、“2_...”),但这真的很乏味。
ggplot(MyData, aes(x=competence,y=(..count..))) + geom_bar()
此外,我的条形图中有一个 NA-bar,MyData[,c("competence")]
似乎没有用。但那是另外一回事了。
提前致谢!
library(ggplot2)
df
# resp comp
# 1 1 Party A
# 2 2 Party A
# 3 3 Party B
# 4 4 Party B
# 5 5 Party B
# 6 6 Party C
df1 <- data.frame(table(df$comp))
df1
# Var1 Freq
# 1 Party A 2
# 2 Party B 3
# 3 Party C 1
使用factor()
手动排列关卡
df1$Var1 <- factor(df1$Var1, c("Party B", "Party C", "Party A"))
df1
# Var1 Freq
# 2 Party B 3
# 3 Party C 1
# 1 Party A 2
ggplot(df1, aes(x = Var1, y = Freq)) + geom_bar(stat = "identity")
聚会频率按降序排列
df1 <- data.frame(table(df$comp))
df1
# Var1 Freq
# 1 Party A 2
# 2 Party B 3
# 3 Party C 1
df1 <- df1[order(df1$Freq, decreasing=TRUE),]
df1
# Var1 Freq
# 2 Party B 3
# 1 Party A 2
# 3 Party C 1
ggplot(df1, aes(x = Var1, y = Freq)) + geom_bar(stat = "identity")
根据您是否需要降序,您可以简单地使用 dplyr
和 reorder
。
library(dplyr)
library(ggplot2)
count(df, competence) %>%
ggplot(aes(x = reorder(competence, -n), y = n)) +
geom_col()
我想重新排列我的 ggplot 条形图的条形 - 在 Whosebug 上有很多类似的条目(例如 here)。
但是,我的问题是:您能否通过告诉 ggplot 不要按标签的字母顺序排序而是按相同标签的计数作为感兴趣的价值。
就我而言,我有关于哪个政党拥护某个问题/在给定问题领域最有能力的问题的调查数据。
respondent-id competence
1 "Party A"
2 "Party A"
3 "Party B"
4 "Party B"
5 "Party B"
6 "Party C"
ggplot 现在要做的是一个条形图,其中有第二高的第一个(甲方),第二高的(乙方)和最后一个最低的(丙方)。但是我如何告诉 ggplot 将计数考虑在内(2:3:1 --> 将 B 方放在第一位)?
我按照建议 here 尝试了几种方法,但这并没有解决问题:大多数方法都包含一个位置变量,它会告诉 ggplot "assign party B first place"。我还尝试通过 "competence" 来 reorder()
,但没有成功。最后,我可以为各方分配不同的前缀(“1_party_B”、“2_...”),但这真的很乏味。
ggplot(MyData, aes(x=competence,y=(..count..))) + geom_bar()
此外,我的条形图中有一个 NA-bar,MyData[,c("competence")]
似乎没有用。但那是另外一回事了。
提前致谢!
library(ggplot2)
df
# resp comp
# 1 1 Party A
# 2 2 Party A
# 3 3 Party B
# 4 4 Party B
# 5 5 Party B
# 6 6 Party C
df1 <- data.frame(table(df$comp))
df1
# Var1 Freq
# 1 Party A 2
# 2 Party B 3
# 3 Party C 1
使用factor()
df1$Var1 <- factor(df1$Var1, c("Party B", "Party C", "Party A"))
df1
# Var1 Freq
# 2 Party B 3
# 3 Party C 1
# 1 Party A 2
ggplot(df1, aes(x = Var1, y = Freq)) + geom_bar(stat = "identity")
聚会频率按降序排列
df1 <- data.frame(table(df$comp))
df1
# Var1 Freq
# 1 Party A 2
# 2 Party B 3
# 3 Party C 1
df1 <- df1[order(df1$Freq, decreasing=TRUE),]
df1
# Var1 Freq
# 2 Party B 3
# 1 Party A 2
# 3 Party C 1
ggplot(df1, aes(x = Var1, y = Freq)) + geom_bar(stat = "identity")
根据您是否需要降序,您可以简单地使用 dplyr
和 reorder
。
library(dplyr)
library(ggplot2)
count(df, competence) %>%
ggplot(aes(x = reorder(competence, -n), y = n)) +
geom_col()