ggridges - опустите первую строку данных в нижнюю часть графика

У меня вопрос относительно ggplot2 и ggridges. Я хочу построить те же экспериментальные данные XRD. Как я могу опустить первые данные в конец графика, не затрагивая третий график (см. рисунок). Вот данные.

# 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()

Сюжет

Большое спасибо за помощь.


person heisenberg_px    schedule 14.10.2020    source источник


Ответы (2)


Если вы масштабируете/расширяете ось x/y, вы можете уменьшить «зазор». Вот пример с набором данных gapminder:

## 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

Итак, для вашей проблемы что-то вроде этого должно быть хорошим решением:

# 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()
  • Если вам нужна дополнительная помощь в устранении неполадок, было бы полезно опубликовать некоторые из ваших фактических данных.
person jared_mamrot    schedule 15.10.2020
comment
Благодарю вас! Переключение вниз выглядит очень хорошо. К сожалению, верхний график (образец) слишком близок к рамке. Похоже, что данные обрезаются в кадре. Я добавил данные к вопросу. - person heisenberg_px; 15.10.2020

Я нашел ответ. Добавление аргумента add к expansion сделало работу.

# 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()

Сюжет

person heisenberg_px    schedule 15.10.2020