根据其中一个类别的数值对具有两个类别的图重新排序 geom_bar
Reorder geom_bar plot with two categories by the numerical value of just one of those categories
我意识到这个问题的标题可能有点混乱,我想解决的问题如下。假设我有以下数据框:
total_cash_and_card <- data.frame(amount_type=as.character(c('cash', 'cash', 'cash', 'cash', 'cash', 'cash', 'card', 'card', 'card', 'card', 'card', 'card')),
user_id=as.character(c('Blah_1', 'Blah_17', 'abcd1', 'user27', 'foobar', 'scrum', 'abcd1', 'foobar', 'Blah_1', 'Blah_17', 'user27', 'scrum')),
total=as.numeric(c(47,21,17,9,2,1,55,45,32,12,10,3)),
stringsAsFactors=FALSE)
然后我生成了以下图(使用以下代码):
g = ggplot(data=total_cash_and_card, aes(x = user_id, y = total, fill=amount_type)) +
geom_bar(colour="black", stat="identity",
position=position_dodge(),
size=.3) +
labs(x="", y="Amount", caption="(based on sample data)", title='Cash Amount vs Card Amount') +
theme_few(base_size = 10) +
scale_color_few() +
theme(axis.text.x=element_text(angle = 45, hjust = 1))
#theme(
#axis.text.y = element_blank())
#axis.ticks.y = element_blank())
p = ggplotly(g)
p = layout(p, margin = m, showlegend = TRUE)
p
我如何重新排序我的情节,以便 x-axis(又名 user_id)根据属于 'card' 的 'total' 从左到右排序] 类别(由 amount_type 列表示)? (即我如何重新排列我的图,以便红色条从最大到最小,从左到右移动?)。希望我的问题足够清楚,在此先感谢您的帮助!
我删除了需要额外软件包的 theme_few
和 ggplotly
(根据 eipi10 的评论)。在这种情况下,您需要做的是确定您想要 user_id
的顺序,然后使用 factor
应用该顺序。
library(tidyverse) #for dplyr and ggplot2
order <- total_cash_and_card %>%
filter(amount_type == "card") %>%
pull(user_id)
ggplot(data=total_cash_and_card, aes(x = factor(user_id, order), y = total, fill=amount_type)) +
geom_col(colour="black", position=position_dodge(), size=.3) +
labs(x="", y="Amount", caption="(based on sample data)", title='Cash Amount vs Card Amount')
我意识到这个问题的标题可能有点混乱,我想解决的问题如下。假设我有以下数据框:
total_cash_and_card <- data.frame(amount_type=as.character(c('cash', 'cash', 'cash', 'cash', 'cash', 'cash', 'card', 'card', 'card', 'card', 'card', 'card')),
user_id=as.character(c('Blah_1', 'Blah_17', 'abcd1', 'user27', 'foobar', 'scrum', 'abcd1', 'foobar', 'Blah_1', 'Blah_17', 'user27', 'scrum')),
total=as.numeric(c(47,21,17,9,2,1,55,45,32,12,10,3)),
stringsAsFactors=FALSE)
然后我生成了以下图(使用以下代码):
g = ggplot(data=total_cash_and_card, aes(x = user_id, y = total, fill=amount_type)) +
geom_bar(colour="black", stat="identity",
position=position_dodge(),
size=.3) +
labs(x="", y="Amount", caption="(based on sample data)", title='Cash Amount vs Card Amount') +
theme_few(base_size = 10) +
scale_color_few() +
theme(axis.text.x=element_text(angle = 45, hjust = 1))
#theme(
#axis.text.y = element_blank())
#axis.ticks.y = element_blank())
p = ggplotly(g)
p = layout(p, margin = m, showlegend = TRUE)
p
我如何重新排序我的情节,以便 x-axis(又名 user_id)根据属于 'card' 的 'total' 从左到右排序] 类别(由 amount_type 列表示)? (即我如何重新排列我的图,以便红色条从最大到最小,从左到右移动?)。希望我的问题足够清楚,在此先感谢您的帮助!
我删除了需要额外软件包的 theme_few
和 ggplotly
(根据 eipi10 的评论)。在这种情况下,您需要做的是确定您想要 user_id
的顺序,然后使用 factor
应用该顺序。
library(tidyverse) #for dplyr and ggplot2
order <- total_cash_and_card %>%
filter(amount_type == "card") %>%
pull(user_id)
ggplot(data=total_cash_and_card, aes(x = factor(user_id, order), y = total, fill=amount_type)) +
geom_col(colour="black", position=position_dodge(), size=.3) +
labs(x="", y="Amount", caption="(based on sample data)", title='Cash Amount vs Card Amount')