0

I have the following pieces of code:

library("ggplot2") 

x1 = as.numeric(x=QualidadeARO3$Ilhavo)
ggplot (QualidadeARO3, aes(x=x1, color="Ílhavo")) +
  geom_histogram(fill="black", position="dodge") +
  theme(legend.position="top") + 
   xlab("microgramas por metro cúbico") + 
   ylab("horas") 

and

library("ggplot2") 
x2 = as.numeric(x=QualidadeARO3$VNTelha_Maia)

ggplot(QualidadeARO3, aes(x=x2, color="VN Telha-Maia")) +
  geom_histogram(fill="blue", position="dodge")+
  theme(legend.position="top") +
  xlab("microgramas por metro cúbico") + 
  ylab("horas") 

Where QualidadeARO3 is a data sheet imported from Excel that looks like this:

enter image description here

And both pieces of code output the following graphs, respectively:

enter image description here enter image description here

However, I want to merge both graphs into a single mirrored histogram, with both color labels on top, how do I do this?

i.e., something like this: enter image description here

  • 1
    @r2evans While I certainly agree with the advice to new users, I don't understand why the question wasn't closed as a duplicate of the cited question. – IRTFM May 11 '22 at 21:28
  • @r2evans thank you for the link and response, but no. – A Burial At Ornans May 11 '22 at 22:23
  • @IRTFM, I chose to delay the dupe-hammer, hoping others may weigh in with votes if desired. Sometimes I want to _vote_ to close as a dupe instead of single-handedly hammering it. – r2evans May 11 '22 at 22:24
  • ABurialAtOrnans, would you prefer to share your data or have a suggestion using a different dataset? I'm not going to transcribe from a picture of your data. (Relevant: see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info, and see discussion about using `dput`, `read.table`, or other means of providing unambiguous data in a question. Thanks!) – r2evans May 11 '22 at 22:24
  • @r2evans here is the complete data sheet: https://docs.google.com/spreadsheets/d/1HfGKUktbU3dHLEOWhzUR_PS_kFUpy0RL/edit?usp=sharing&ouid=108079078123985588678&rtpof=true&sd=true – A Burial At Ornans May 11 '22 at 23:10

1 Answers1

0
ggplot(dat) +
  geom_histogram(aes(x = Ilhavo, y = ..density..), fill = "gray", color = "black") +
  geom_histogram(aes(x = VNTelha_Maia, y = -..density..), fill = "gray60", color = "black") +
  geom_hline(yintercept = 0)
# `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Warning: Removed 1 rows containing non-finite values (stat_bin).
# `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Warning: Removed 1 rows containing non-finite values (stat_bin).

dual histogram

r2evans
  • 141,215
  • 6
  • 77
  • 149