ggplot2 将多种图表类型组合成单个图表
ggplot2 combining multiple chart types into single chart
我是 R 的新手,对 ggplot2 和 plotly 的组合很着迷。在下面的代码中,我试图用数据集中的 WAMP 标签绘制箱线图抖动。我似乎得到了大部分,除了我无法按大小对 x 轴进行排序并显示 2 个 x 轴值,类似于这个问题:Multi-row x-axis labels in ggplot line chart
这是我的 R 代码:
library(ggplot2)
library(plotly)
df <- data.frame(Products = rep(c('Product1','Product2','Product3'), times = 100),
Size = rep(c(1000,3000,2000), times = 100),
sales = runif(100, min=0, max = 300),
WAMP = rep(c(1.5,2.5,3.5), times = 100),
PricePerUnit = runif(100, min = 1, max = 5))
p <- ggplot() +
geom_boxplot(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, group = 1), outlier.colour = NULL) +
geom_jitter(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, color = sales)) +
scale_color_gradientn(colours = c("red", "yellow", "green", "lightblue", "darkblue")) +
scale_y_continuous(breaks = seq(0, 10, by = .5)) +
geom_line(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP)) +
geom_text(data = df, aes(label = round(WAMP,2), x = Products, y = WAMP), size = 3) +
ggtitle("Market by Price Distribution and Sales Volume")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
p <- ggplotly(p)
p
它生成此图像,如您所见,它将 WAMP 图表放在箱线图抖动(未合并)旁边。在这里非常感谢任何帮助。谢谢
Boxplot Jitter
我不太确定我明白了,但也许这就是你想要的?
p <- ggplot() +
geom_boxplot(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, group = 1), outlier.colour = NULL) +
geom_jitter(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, color = sales)) +
scale_color_gradientn(colours = c("red", "yellow", "green", "lightblue", "darkblue")) +
scale_y_continuous(breaks = seq(0, 10, by = .5)) +
geom_line(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP)) +
geom_text(data = df, aes(label = round(WAMP,2), x = interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP), size = 3) +
ggtitle("Market by Price Distribution and Sales Volume")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
p <- ggplotly(p)
p
只是将 geom_text
指定到右侧 x
的问题。
我是 R 的新手,对 ggplot2 和 plotly 的组合很着迷。在下面的代码中,我试图用数据集中的 WAMP 标签绘制箱线图抖动。我似乎得到了大部分,除了我无法按大小对 x 轴进行排序并显示 2 个 x 轴值,类似于这个问题:Multi-row x-axis labels in ggplot line chart
这是我的 R 代码:
library(ggplot2)
library(plotly)
df <- data.frame(Products = rep(c('Product1','Product2','Product3'), times = 100),
Size = rep(c(1000,3000,2000), times = 100),
sales = runif(100, min=0, max = 300),
WAMP = rep(c(1.5,2.5,3.5), times = 100),
PricePerUnit = runif(100, min = 1, max = 5))
p <- ggplot() +
geom_boxplot(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, group = 1), outlier.colour = NULL) +
geom_jitter(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, color = sales)) +
scale_color_gradientn(colours = c("red", "yellow", "green", "lightblue", "darkblue")) +
scale_y_continuous(breaks = seq(0, 10, by = .5)) +
geom_line(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP)) +
geom_text(data = df, aes(label = round(WAMP,2), x = Products, y = WAMP), size = 3) +
ggtitle("Market by Price Distribution and Sales Volume")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
p <- ggplotly(p)
p
它生成此图像,如您所见,它将 WAMP 图表放在箱线图抖动(未合并)旁边。在这里非常感谢任何帮助。谢谢
Boxplot Jitter
我不太确定我明白了,但也许这就是你想要的?
p <- ggplot() +
geom_boxplot(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, group = 1), outlier.colour = NULL) +
geom_jitter(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), PricePerUnit, color = sales)) +
scale_color_gradientn(colours = c("red", "yellow", "green", "lightblue", "darkblue")) +
scale_y_continuous(breaks = seq(0, 10, by = .5)) +
geom_line(data = df, aes(interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP)) +
geom_text(data = df, aes(label = round(WAMP,2), x = interaction(df$Size, df$Products, lex.order = TRUE), y = WAMP), size = 3) +
ggtitle("Market by Price Distribution and Sales Volume")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
p <- ggplotly(p)
p
只是将 geom_text
指定到右侧 x
的问题。