I have two columns of data "Edge_MS" and "Inside" that I want to wrap into two histograms and stack them on top of each other.
Using ggplot(...) and then facet_wrap(...), what are the specific steps I need to follow to "stack" these two histograms.
Hi!
To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
Hi @Bro,
Sounds like you could use the patchwork
package to stack your two histograms.
Here's a very crude example of how that works:
install.packages(c("patchwork", "ggplot2", "tidyverse"))
library(patchwork)
library(ggplot2)
library(tidyverse)
# Create some dummy data
set.seed(1234)
my_data <- data.frame(Edge_MS = c(sample(1:100, 50, replace=TRUE)),
Inside = c(sample(10:50, 50, replace=TRUE)))
head(my_data)
my_data %>%
ggplot(., aes(x=Edge_MS)) + geom_histogram(binwidth=10, fill="blue") -> p1
my_data %>%
ggplot(., aes(x=Inside)) + geom_histogram(binwidth=10, fill="red") -> p2
p1
p2
p1 / p2
p.s. I hope this is not just "doing your homework for you" !
HTH
data.frame( Edge_MS = c(19.5, 17.9, 18.2, 17, 18.65, 14.5),
Inside = c(18.2, 13.1, 14, 17.8, 16.75, 13.6)
library(ggplot2)
ggplot(DBH1, aes(x = ?)) +
geom_histogram(binwidth = 2000, color = "grey30", fill = "white") +
facet_grid(? ~ .)
an excellent solution, thanks DavoWW
For using face_grid()
you have to reshape your data, see this example
library(tidyverse)
DBH1 <- data.frame(Edge_MS = c(19.5, 17.9, 18.2, 17, 18.65, 14.5),
Inside = c(18.2, 13.1, 14, 17.8, 16.75, 13.6))
DBH1 %>%
gather(variable, value) %>%
ggplot(aes(x = value)) +
geom_histogram(color = "grey30", fill = "white") +
facet_grid(rows = vars(variable))
Thanks for your solution, really grateful for the time.
Plus, this is for independent research, no HW.
Thanks,
Bro.
So I followed your directions and plugged in the code, but even after I organize my data, I get this error.
"Error in FUN(X[[i]], ...) : object 'value' not found"
What should I do?
Thanks for your time and help,
Bro
Can you provide a complete reprex for this? "value" should be created on this step
Absolutely,
data.frame( Edge_MS = c(19.5, 17.9, 18.2, 17, 18.65, 14.5),
Inside = c(18.2, 13.1, 14, 17.8, 16.75, 13.6)
library(ggplot2)> DBH1 %>%
gather(variable, vlaue) %>%
ggplot(aes(x = value))+
- geom_histogram(color = "grey30", fill = "white") +
- facet_grid(rows = vars(variable))
Error in FUN(X[[i]], ...) : object 'value' not found
...when I just use the gather command/function, I do get this
DBH1 %>%
gather(variable, vlaue)
variable vlaue
1 Edge_MS 19.50
2 Edge_MS 17.90
3 Edge_MS 18.20
4 Edge_MS 17.00
5 Edge_MS 18.65
6 Edge_MS 14.50
What am I doing wrong?
Thanks,
Bro.
You have a typo here vlaue instead of value
Whoops, I am new at this.
Really thankful for your help, this really helps me out.
Bro.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.