0

So I have this code

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

and have this graph:

first

Also I want to add the jitter on the boxplots. But if I just add

  + geom_jitter(alpha=0.5, aes(price, values, color=group), 
            position = position_jitter(width = .2))

I'm having the jitter on the lines geom_line, but not on the boxplots.

second

Is that possible?


P.S.: the whole chunk of code with data 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")
vdi
  • 743
  • 10
  • 20
  • This is a weird way to set up your ggplot calls, and as a result you are getting undesired values for `x` in the different geoms. Why are you passing string literals to `x` in the `geom_boxplot` calls? – arvi1000 Jan 28 '15 at 05:35
  • @arvi1000 yeah, I wanted to delete mess and removed the data-part. In addition, I used the example [here](http://stackoverflow.com/questions/15605926/how-to-draw-a-plot-joining-points-from-two-measurement-times) so I just modified it. This leads to the problem with the alphabetical order of `x`. That is why I also have this strange "a_", "b_" and so on. – vdi Jan 28 '15 at 06:28
  • Can't get the data from the above, because you didn't post the contents of `nb_firm_two_price`... – arvi1000 Jan 28 '15 at 15:20
  • @arvi1000 I did not intend to :) but okay [Google Drive, .csv, 25 kB](https://drive.google.com/file/d/0B99IyjVoTPo6UXdlcGE5RWZOM0k/view?usp=sharing) – vdi Jan 28 '15 at 15:54

1 Answers1

0

Here's an example of how to do what I think you are going for, using reproducible data.

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

Result:

enter image description here

arvi1000
  • 9,393
  • 2
  • 42
  • 52