ggridges - lower the first dataline to bottom of plot

337 Views Asked by At

I have a question regarding ggplot2 and ggridges. I want to plot same experimental XRD data. How am I able to lower the first data to the bottom of the plot without affecting the third plot (see picture). Here is the data

# Data import
data_celestine <- read.table('../Data/celestine.asc')
data_barite <- read.table('../Data/barite.asc')
data_sample <- read.table('../Data/sample.asc')

# Plot
df <- data.frame(
  x=data_celestine$V1,
  y=c(data_sample$V2, data_celestine$V2, data_barite$V2),
  samplename=c(rep('Sample', length(data_celestine$V1)), rep('Celestine',length(data_celestine$V1)), rep('Barite',length(data_celestine$V1))))

library(ggplot2)
library(ggridges)

p <- ggplot(df, aes(x,y, color=samplename))
p + geom_ridgeline(
  aes(y=samplename, height=y),
  fill=NA, scale=.00004, min_height=-Inf) +
  theme_bw()

Plot

Thank you very much for helping me.

2

There are 2 best solutions below

0
On

I found the answer. Adding the argument add to expansion did the job.

# Data import
data_celestine <- read.table('../Data/celestine.ASC')
data_barite <- read.table('../Data/barite.asc')
data_sample <- read.table('../Data/sample.asc')

# Plot
library(ggplot2)
library(ggridges)

df <- data.frame(
          x=data_celestine$V1,
          y=c(data_sample$V2, data_celestine$V2, data_barite$V2),
          sample_name=c(rep('Sample', length(data_celestine$V1)), rep('Celestine',length(data_celestine$V1)), rep('Barite',length(data_celestine$V1))))

p <- ggplot(df, aes(x,y, color=sample_name))
p + geom_ridgeline(
  aes(y=sample_name, height=y),
  fill=NA, scale=.000045, min_height=-Inf) +
  scale_y_discrete(expand = expansion(add = c(0.025, 0.6))) +
  theme_bw()

Plot

1
On

If you scale/expand your x/y axis you can reduce the 'gap'. Here is an example with the gapminder dataset:

## No 'expand'
library(ggplot2)
library(ggridges)

data_url = 'https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv'
gapminder = read.csv(data_url)
ggplot(gapminder, aes(y=as.factor(year),
                      x=lifeExp)) +
  geom_density_ridges() +
  theme(axis.text=element_text(size=20))

example1.png

## With 'expand'
library(ggplot2)
library(ggridges)

data_url = 'https://raw.githubusercontent.com/resbaz/r-novice-gapminder-files/master/data/gapminder-FiveYearData.csv'
gapminder = read.csv(data_url)
ggplot(gapminder, aes(y=as.factor(year),
                      x=lifeExp)) +
  geom_density_ridges() +
  scale_y_discrete(expand = c(0.01, 0)) +  
  scale_x_continuous(expand = c(0, 0))+
  theme(axis.text=element_text(size=20))

example_2.png

So, for your problem, something like this should be a good solution:

# Data import
data_celestine <- read.table('../Data/celestine.asc')
data_barite <- read.table('../Data/barite.asc')
data_sample <- read.table('../Data/sample.asc')

# Plot
df <- data.frame(
  x=data_celestine$V1,
  y=c(data_sample$V2, data_celestine$V2, data_barite$V2),
  samplename=c(rep('Sample', length(data_celestine$V1)), rep('Celestine',length(data_celestine$V1)), rep('Barite',length(data_celestine$V1))))

library(ggplot2)
library(ggridges)

p <- ggplot(df, aes(x,y, color=samplename))
p + geom_ridgeline(
  aes(y=samplename, height=y),
  fill=NA, scale=.00004, min_height=-Inf) +
  scale_y_discrete(expand = c(0.01, 0)) +
  theme_bw()
  • If you need further help troubleshooting it would be helpful to post some of your actual data.