0

I have 2 histograms which i would like to combine into one plot. Somehow i cannot add both of them together. One data frame has length of 1000 and the other has a length of 1000.

The following code gives me an error:

Error: `mapping` must be created by `aes()`

How can i go about combining them with a legend?

p <-ggplot(prediction_3)+
geom_histogram(aes(x=prediction_3), binwidth = 0.01)

p + geom_histogram(prediction_2b, aes(x=prediction),binwidth = 0.01, fill = "red", alpha = 0.7)+
geom_vline(xintercept=prediction_1)+
geom_text(aes(0.5,prediction_1,label = 0.469, vjust = 1))

The individual histogram plots are as follows:

1000 values: enter image description here

10000 values: enter image description here

Any help will be appreciated. thank you

EDIT:

prediction_2b$value <- 'prediction_2b' 
prediction_3$value <- 'prediction_3' 
combined_pred <- rbind(prediction_2b, prediction_3)

an error appears: Error in match.names(clabs, names(xi)) : names do not match previous names

MY.BK
  • 167
  • 1
  • 2
  • 12
  • https://stackoverflow.com/questions/3541713/how-to-plot-two-histograms-together-in-r has a solution – Jonny Phelps Apr 18 '19 at 13:30
  • 1
    Possible duplicate of [Plotting two variables as lines using ggplot2 on the same graph](https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph) – camille Apr 18 '19 at 13:34
  • I tried this: `prediction_2b$value <- 'prediction_2b' prediction_3$value <- 'prediction_3' combined_pred <- rbind(prediction_2b, prediction_3)` but an error appears: Error in match.names(clabs, names(xi)) : names do not match previous names – MY.BK Apr 18 '19 at 13:40
  • is this what you're looking for? https://stackoverflow.com/questions/49778580/how-to-stack-two-histograms-in-one-with-ggplot2-in-r/49779033#49779033 – tomasu Apr 18 '19 at 13:42
  • @tomasu hi there, my data frame have different lengths (1000 and 10000). So far, the examples i have seen have same lengths of data. – MY.BK Apr 18 '19 at 13:43
  • You can [edit] your question to put code there, since it's difficult to format well in comments – camille Apr 18 '19 at 13:43

2 Answers2

2

What about this, using some fake data, due there are not yours:

library(tidyverse)
# fake data
prediction_1 = 0.469

prediction_3 <- data.frame(prediction_3 = rnorm(1000, 4, 3))  
prediction_2b <- data.frame(prediction = rnorm(10000, 8, 3))

Here the separated plots:

ggplot(prediction_3)+
  geom_histogram(aes(x=prediction_3), binwidth = 0.01)

ggplot(prediction_2b)+
  geom_histogram(aes(x=prediction), binwidth = 0.01)

To plot them together, here you can manually melt them in the long format:

dats <- rbind(data.frame(pred = prediction_3$prediction_3, var = 'prediction_3'),
              data.frame(pred = prediction_2b$pred, var = 'prediction_2b'))

# here the plot
ggplot(dats, aes(pred, fill = var)) + 
  geom_histogram(alpha = 0.5, position = "identity", bins = 75) +
  geom_vline(xintercept=prediction_1) 

enter image description here

s__
  • 9,270
  • 3
  • 27
  • 45
0

Try to combine the 2 data.frame with a variable that indicates their difference.

If the 2 data.frame predition_3 and prediction_2b have the same column names you can do:

prediction_3$prediction_no <- '3'
prediction_2b$prediction_no <- '2b'
prediction.table <- rbind(prediction_2b, prediction_3)

Then you can use the fill aesthetic to separate data into 2 histograms:

p <-ggplot(prediction.table)
p + geom_histogram(aes(x=prediction, fill=prediction_no), binwidth = 0.01, alpha=0.7)
p + scale_fill_manual(values=c('red', 'blue')) # use your own instead of default colors
p + geom_vline(xintercept=prediction_1) 
# p + geom_text(aes(0.5,prediction_1,label = 0.469, vjust = 1)) 
# I suggest to move any static assignments out of the aes() call!
# assuming that prediction_1 is a single value you can do
p + geom_text(y=0.5, y=prediction_1, label = 0.469, vjust = 1)
Simon
  • 577
  • 3
  • 9
  • Hi there thank you! But rbind gives me an error: `Error in match.names(clabs, names(xi)) : names do not match previous names` – MY.BK Apr 18 '19 at 13:44
  • HI there, i tried renaming the values to `prediction` and plotted it but the histogram seems inaccurate. does rbind add values to the data frame that has 1000 values? – MY.BK Apr 18 '19 at 14:01
  • `rbind()` (`rowbind()`) adds the second data.frame below the first one. So it does not add data that was not there before. Post the output of `dput(prediction_3)` and `dput(prediction_2b)` and I will try to figure our how to do it. – Simon Apr 18 '19 at 18:40
  • Hi @Simon there are 10000 observations in `prediction_3` and 1000 in `prediction_2b`. I shall post the first few rows of each? – MY.BK Apr 19 '19 at 00:36
  • Just post the output of `dput(head(prediction_3))`and `dput(head(prediction_2b))`. – Simon Apr 19 '19 at 10:49