1

我有一个关于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()

阴谋

非常感谢你帮助我。

4

2 回答 2

0

我找到了答案。添加参数addexpansion完成这项工作。

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

阴谋

于 2020-10-15T08:56:23.623 回答
0

如果您缩放/扩展您的 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))

例子1.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))

示例_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()
  • 如果您需要进一步的故障排除帮助,发布一些实际数据会很有帮助。
于 2020-10-15T03:15:47.993 回答