简体   繁体   中英

two histograms in one plot (ggplot)

Well, I've been looking in this site to make two histograms in one plot. I get to

ggplot()+geom_histogram(data=etapa1, aes(x=AverageTemperature),col="red")+
geom_histogram(data=etapa2, aes(x=AverageTemperature),col="blue")

I've got two histograms with different colours, but I don't get a legend or a label which shows which is each colour. How can I produce it?

As Spacedman said it would be better if you could specify your problem more in detail and give an example data set.

So i create a random sample set which simulates a temperature.

etapa1 <- data.frame(AverageTemperature = rnorm(100000, 16.9, 2))
etapa2 <- data.frame(AverageTemperature = rnorm(100000, 17.4, 2))

#Now, combine your two dataframes into one.  First make a new column in each.
etapa1$e <- 'etapa1'
etapa2$e <- 'etapa2'

# combine the two data frames etapa1 and etapa2
combo <- rbind(etapa1, etapa2)

ggplot(combo, aes(AverageTemperature, fill = e)) + geom_density(alpha = 0.2)

For me it seems more obvious to use a density plot rather than a histogram since temperatures are real numbers.

Hope this helps somehow...

If you don't want to combine the two data.frames it is a bit more tricky... You have to use scale_colour_manual and scale_fill_manual . And then define a variable for the fill statement. This can be linked in the labels section

ggplot() + 
  geom_density(data = etapa1, aes(x = AverageTemperature, fill = "r"), alpha = 0.3) +
  geom_density(data = etapa2, aes(x = AverageTemperature, fill = "b"), alpha = 0.3) +
  scale_colour_manual(name ="etapa", values = c("r" = "red", "b" = "blue"), labels=c("b" = "blue values", "r" = "red values")) +
  scale_fill_manual(name ="etapa", values = c("r" = "red", "b" = "blue"), labels=c("b" = "blue values", "r" = "red values"))

You can replace geom_density() with geom_histogram() respectively.

Using @TimoWagner's example:

set.seed(1001)
etapa1 <- data.frame(AverageTemperature = rnorm(100000, 16.9, 2))
etapa2 <- data.frame(AverageTemperature = rnorm(100000, 17.4, 2))

Here's another way to pack the two data sets together:

combdat <- dplyr::bind_rows(list(dat1=etapa1,dat2=etapa2),
                           .id="dataset")

Two superimposed histograms:

library(ggplot2)
ggplot(combdat,aes(AverageTemperature,fill=dataset))+
   scale_fill_manual(values=c("red","blue"))+
   geom_histogram(alpha=0.5,binwidth=0.1,position="identity")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM