ggplot2。如何在不同的图形上添加抖动
ggplot2. How to add jitter on the different graph
所以我有这个代码
ggplot(data, aes(x=price, y=values)) +
# add colourful lines
geom_line(aes(group=group, color=group)) +
# add two box plots
geom_boxplot(data=df, aes(x="z_lnprice_new", y=x_lnprice_new)) +
geom_boxplot(data=df, aes(x='a_lnprice_new', y=b_lnprice_old)) +
# delete the legend
theme(legend.position="none")
并有这张图:
我还想在箱线图上添加 jitter
。但是如果我只是添加
+ geom_jitter(alpha=0.5, aes(price, values, color=group),
position = position_jitter(width = .2))
我在线条 geom_line
上有抖动,但在箱线图上没有。
这可能吗?
P.S.: 包含数据 Google Drive, .csv, 25 kB
的整个代码块
df <- data.frame(b_lnprice_old= sort(nb_firm_two_price[[175]]),
x_lnprice_new = sort(nb_firm_two_price[[176]]))
data <- data.frame(group = factor(1:nrow(df)),
price=c(rep('b_lnprice_old',nrow(df)),
rep('x_lnprice_old',nrow(df))),
values=c(df$b_lnprice_old,df$x_lnprice_new))
ggplot(data, aes(x=price, y=values)) +
geom_line(aes(group=group, color=group)) +
geom_boxplot(data=df, aes(x="z_lnprice_new", y=x_lnprice_new)) +
geom_boxplot(data=df, aes(x='a_lnprice_new', y=b_lnprice_old)) +
theme(legend.position="none")
下面是一个示例,说明如何使用可重现的数据来完成我认为您想要做的事情。
# reproducible random data
set.seed(123)
obs <- 100
dat <- data.frame(group=1:obs, old_price=rnorm(obs, mean=10))
dat$new_price <- dat$old_price + rnorm(obs, mean=1)
# libraries
library(reshape2)
library(ggplot2)
# convert from wide to long
plot_dat <- melt(dat, id.var='group')
# plot
ggplot(plot_dat) +
# simple lines
geom_line(aes(x=variable, y=value, group=group, color=group)) +
# box plots and jitter points, with modified x value
geom_boxplot(aes(x=paste0('hist_', variable), y=value)) +
geom_jitter(aes(x=paste0('hist_', variable), y=value, color=group)) +
# specify x value order
scale_x_discrete(limits=c('hist_old_price', 'old_price', 'new_price',
'hist_new_price'))
结果:
所以我有这个代码
ggplot(data, aes(x=price, y=values)) +
# add colourful lines
geom_line(aes(group=group, color=group)) +
# add two box plots
geom_boxplot(data=df, aes(x="z_lnprice_new", y=x_lnprice_new)) +
geom_boxplot(data=df, aes(x='a_lnprice_new', y=b_lnprice_old)) +
# delete the legend
theme(legend.position="none")
并有这张图:
我还想在箱线图上添加 jitter
。但是如果我只是添加
+ geom_jitter(alpha=0.5, aes(price, values, color=group),
position = position_jitter(width = .2))
我在线条 geom_line
上有抖动,但在箱线图上没有。
这可能吗?
P.S.: 包含数据 Google Drive, .csv, 25 kB
的整个代码块df <- data.frame(b_lnprice_old= sort(nb_firm_two_price[[175]]),
x_lnprice_new = sort(nb_firm_two_price[[176]]))
data <- data.frame(group = factor(1:nrow(df)),
price=c(rep('b_lnprice_old',nrow(df)),
rep('x_lnprice_old',nrow(df))),
values=c(df$b_lnprice_old,df$x_lnprice_new))
ggplot(data, aes(x=price, y=values)) +
geom_line(aes(group=group, color=group)) +
geom_boxplot(data=df, aes(x="z_lnprice_new", y=x_lnprice_new)) +
geom_boxplot(data=df, aes(x='a_lnprice_new', y=b_lnprice_old)) +
theme(legend.position="none")
下面是一个示例,说明如何使用可重现的数据来完成我认为您想要做的事情。
# reproducible random data
set.seed(123)
obs <- 100
dat <- data.frame(group=1:obs, old_price=rnorm(obs, mean=10))
dat$new_price <- dat$old_price + rnorm(obs, mean=1)
# libraries
library(reshape2)
library(ggplot2)
# convert from wide to long
plot_dat <- melt(dat, id.var='group')
# plot
ggplot(plot_dat) +
# simple lines
geom_line(aes(x=variable, y=value, group=group, color=group)) +
# box plots and jitter points, with modified x value
geom_boxplot(aes(x=paste0('hist_', variable), y=value)) +
geom_jitter(aes(x=paste0('hist_', variable), y=value, color=group)) +
# specify x value order
scale_x_discrete(limits=c('hist_old_price', 'old_price', 'new_price',
'hist_new_price'))
结果: