R:如何构建具有多个条形图和双轴的条形图?
R: How to build barplot with multiple bars and dual axis?
我的数据框有 1 个分类变量和 3 个数值变量。我想在 plotly R 中构建条形图,其中 'share' 和 'sale' 变量将以条形给出并反映在左侧 y 轴上,而 'cost' 变量将按线可视化并反映单位在右边的第二个轴上。此外,根据 'model' 列
,条形颜色应有所不同
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))
预期输出(Code):
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())
更新 次要 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())
第一个回答:
是这样的吗?
以长格式导入数据
对每个条形和线条过滤数据
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())
我的数据框有 1 个分类变量和 3 个数值变量。我想在 plotly R 中构建条形图,其中 'share' 和 'sale' 变量将以条形给出并反映在左侧 y 轴上,而 'cost' 变量将按线可视化并反映单位在右边的第二个轴上。此外,根据 'model' 列
,条形颜色应有所不同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))
预期输出(Code):
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())
更新 次要 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())
第一个回答: 是这样的吗?
以长格式导入数据
对每个条形和线条过滤数据
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())