如何自定义双轴Barchart?
我有1个CAT和3个数值变量的DF。现在,我想知道如何按“成本”的大小从右到左订购栏(由红线给出)。在我想手动指定每个条形的颜色之后,但找不到道路。另外,是否可以将这些数字(成本)放入框中?
df <- data.frame (model = c("A", "B", "C","D","E","F"),
share = c(12,20,15,9,60,20),
sale = c(16,25,18,14,67,28),
cost = c(14,19,28,24,57,28))
df_long <- df %>%
pivot_longer(
cols = -model
)
ggplot(df_long, aes(x = model, y= value, label=value))+
geom_col(data = filter(df_long, name != "cost"), aes(fill=name), position = position_dodge())+
scale_fill_manual(values = c("blue", "grey"))+
geom_line(data = filter(df_long, name == "cost"), aes(color = name, group = 1), size = 0.5)+
scale_color_manual(values = "red")+
geom_point(data = filter(df_long, name == "cost"), size = 2)+
geom_text(data = filter(df_long, name == "cost"), hjust=0, vjust=-0.5)+
scale_y_continuous(
name = "Sale and Share",
sec.axis = sec_axis(~., name ="Cost")
)+
theme_minimal()+
theme(legend.title=element_blank())
This is a follow-up question of this R: How to build barplot with multiple bars and dual axis?
I have df with 1 cat and 3 numerical variables. Now I would like to know how to order bar descending from the right to left by size of 'cost' (which is given by red line). After I want to specify the colour of every bars manually myself, but could not find the way. Also, is it possible to put this numbers (costs) in boxes?
df <- data.frame (model = c("A", "B", "C","D","E","F"),
share = c(12,20,15,9,60,20),
sale = c(16,25,18,14,67,28),
cost = c(14,19,28,24,57,28))
df_long <- df %>%
pivot_longer(
cols = -model
)
ggplot(df_long, aes(x = model, y= value, label=value))+
geom_col(data = filter(df_long, name != "cost"), aes(fill=name), position = position_dodge())+
scale_fill_manual(values = c("blue", "grey"))+
geom_line(data = filter(df_long, name == "cost"), aes(color = name, group = 1), size = 0.5)+
scale_color_manual(values = "red")+
geom_point(data = filter(df_long, name == "cost"), size = 2)+
geom_text(data = filter(df_long, name == "cost"), hjust=0, vjust=-0.5)+
scale_y_continuous(
name = "Sale and Share",
sec.axis = sec_axis(~., name ="Cost")
)+
theme_minimal()+
theme(legend.title=element_blank())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模型
的顺序可以通过设置因子的级别来完成。更改geom_text
togeom_label
创建的框而不是纯粹显示文本。我不确定您的意思是“。我自己手动指定每个酒吧的颜色”。你是说填充吗?颜色现在由您的因子确定,可以通过scale_fill_manual
更改,但是您已经这样做了。The order of
model
bycost
can be done by setting the levels of the factor. Changinggeom_text
togeom_label
created boxes instead of purely displaying the text. I am not sure what you mean with ". Specify the colour of every bars manually myself". Do you mean the fill? The color is now determined by your factor and can be changed byscale_fill_manual
, but you already do that.更新:用您喜欢的颜色定义向量,然后使用
scale_fill_identity
:第一个答案::
我们可以从
forcats
软件包中使用fct_reorder
,它在tidyverse
Update: Define a vector with colors you like and then use
scale_fill_identity
:First answer:
We could use
fct_reorder
fromforcats
package it is intidyverse