0

(First time R user) So I'm trying to make overlapping histogram plots with different ranges for x axis values I can easily reproduce from an excel file. I feel like I'm on the right track because its plotting both histograms and making an x axis on 3. I cant seem to figure out how to make the axis(3) take the xlim= that I want. Below is the code Im using and a picture

## Open Xl Data into R
library(readxl)
dataset <- read_excel("location holder")
View(dataset)
## DLS subsets
Zone_1_DLS <- subset(dataset$DLS, dataset$Zone==1)
Zone_2_DLS <- subset(dataset$DLS, dataset$Zone==2)
Zone_3_DLS <- subset(dataset$DLS, dataset$Zone==3)
## ROP Subsets
Zone_1_ROP <- subset(dataset$DAVG_ROP, dataset$Zone==1)
Zone_2_ROP <- subset(dataset$DAVG_ROP, dataset$Zone==2)
Zone_3_ROP <- subset(dataset$DAVG_ROP, dataset$Zone==3)
## DLS Histograms
DLS1 <- hist(Zone_1_DLS)
DLS2 <- hist(Zone_2_DLS)
DLS3 <- hist(Zone_3_DLS)
## ROP Histograms
ROP1 <- hist(Zone_1_ROP)
ROP2 <- hist(Zone_2_ROP)
ROP3 <- hist(Zone_3_ROP)
## Plot Zone 1 Histograms
plot( ROP1, col=rgb(1,0,0,1), xlim=c(0,300), ylim = c(0,500))
plot( DLS1, axis(3),col=rgb(0,0,1,1), xlim=c(0,10), add = T)
axis(3,xlim=c(0,10))

Picture

  • can you upload your data set using `dput()`? – tbradley Mar 02 '17 at 23:35
  • @tbradley too big t paste, here is the onedrive link – Drivric Mar 03 '17 at 16:28
  • You should take some time to create a minimum working example that as shown here: [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – tbradley Mar 03 '17 at 16:45
  • also, `xlim` is not a valid argument for the `axis` function. You should try `axis(3, at = c(1:10))` instead – tbradley Mar 03 '17 at 16:47
  • @tbradley I made some example code to see if this might help explain my issue, `set.seed(42) p1 <- hist(rnorm(500,4)) p2 <- hist(rnorm(500,150)) plot( p1, col=rgb(0,0,1,1/4),xlim=c(0,200)) plot( p2, col=rgb(1,0,0,1/4), add=T)` The range of p1 however is limited from (0-10) where as p2 doesn't have a necessarily definable limit. So anytime I `xlim=c(0,10)` I cut off all my p2 data, where as when plotted above all the p1 data is unusable due to it being only 0-10 on an x scale that can be 0-200. – Drivric Mar 10 '17 at 13:54
  • They exist in a 1 to 1 ratio, for every observation of p2 (where the value can be 0-200) there is an observation at p1 where the values can only be (0-10) So i guess my question lies in can i plot them on different axis (1, 3) but have the axis length be the same so that they will plot on the same graph. – Drivric Mar 10 '17 at 13:55

1 Answers1

0

I am not sure if you have to have this graph in the current format you are looking for, but I wouldn't recommend it, as it is very hard to read and possibly misleading. I have put together a different format where you can have your histograms plotted around each other.

Here it is using the rnorm() values you posted in the comments:

set.seed(42) 
p1 <- data.frame(x = rnorm(500,4)) 
p2 <- data.frame(x = rnorm(500,150)) 

p1$cat <- "a"
p2$cat <- "b"


p <- rbind(p1, p2)
p$zone <- c(rep(c(1, 2), 500))


library(ggplot2)
ggplot(p, aes(x = x))+
  facet_grid(zone~cat, scales = "free")+
  geom_histogram(bins = 30)

and that will give you this graph (with a and b representing your DLS and DAVG_ROP value types, respectively and 1 and 2 representing different zones): multiple histograms with independent x axis

As a side note, you will have 3 rows in the graph output as your have 3 zones.

As for your data, this should work to reproduce something similar to the example (I can't be 100% positive, as I didn't get to actually test it on your data, but I am fairly confident). I recommend keeping it all as one dataset as you had it originally and just reorganizing a little.

library(dplyr)
library(ggplot2)
df1 <- dataset %>%
  select(DLS, Zone) %>%
  mutate(x = DLS, cat = "DLS") %>%
  select(x, Zone, cat)

df2 <- dataset %>%
  select(DAVG_ROP, Zone) %>%
  mutate(x = DAVG_ROP, cat = "DAVG_ROP")%>%
  select(x, Zone, cat)

df <- rbind(df1, df2)

ggplot(df, aes(x = x))+
  facet_grid(Zone~cat, scales = "free")+
  geom_histogram()

There may be a more efficient/concise way to do the tidying in the middle there but this works too.

tbradley
  • 2,210
  • 11
  • 20