简体   繁体   English

R:如何构建多条双轴条形图?

[英]R: How to build barplot with multiple bars and dual axis?

my dataframe has 1 categorical and 3 numerical variables.我的 dataframe 有 1 个分类变量和 3 个数值变量。 I want to build barplot in plotly R, where 'share' and 'sale' variables will be given in bars and reflected on left y axis while 'cost' variable will be visualized by line and units will be reflected on second axis on right.我想在 plotly R 中构建条形图,其中 'share' 和 'sale' 变量将以条形给出并反映在左侧 y 轴上,而 'cost' 变量将按线显示,单位将反映在右侧的第二个轴上。 In addition colour of bars should be different according to 'model' column此外,根据“型号”列,条形颜色应有所不同

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))

Expected output ( Code ):预计 output(代号):

在此处输入图像描述

Update2: Just to show how vjust works: Update2:只是为了展示vjust是如何工作的:

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 = 2)+
  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=-6)+
  scale_y_continuous(
    name = "Sale and Share",
    sec.axis = sec_axis(~., name ="Cost")
  )+
  theme_minimal()+
  theme(legend.title=element_blank())

在此处输入图像描述

Update with secondary y axis and numbers for the points:更新次要 y 轴和点数:

library(tidyverse)

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 = 2)+
  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)+
  scale_y_continuous(
    name = "Sale and Share",
    sec.axis = sec_axis(~., name ="Cost")
  )+
  theme_minimal()+
  theme(legend.title=element_blank())

在此处输入图像描述

First answer: Something like this?第一个回答:像这样的?

  1. Bring your data in long format以长格式导入数据

  2. for each bar and line filter the data对于每个条形和线条过滤数据

library(tidyverse)

df_long <- df %>% 
  pivot_longer(
    cols = -model
  )


ggplot() +
  geom_col(data = filter(df_long, name != "cost"), aes(x = model, y= value, fill=name), position = position_dodge())+
  scale_fill_manual(values = c("blue", "grey"))+
  geom_line(data = filter(df_long, name == "cost"), aes(x = model, y= value, color=name, group=1), size = 2)+
  geom_point(data = filter(df_long, name == "cost"), aes(x = model, y= value), size = 2)+
  scale_color_manual(values = "red")+
  theme_minimal()+
  theme(legend.title=element_blank())

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM