如何使ggplot2图更漂亮?

时间:2021-11-15 14:55:29

I have generated the following plot using the R code that follows it: 如何使ggplot2图更漂亮?

我使用后面的R代码生成了以下图:

ggplot(lengths, aes(length, fill = library)) + geom_density(alpha = 0.2) + coord_cartesian(xlim = c(0, 60000)) 

Now I would like to make the plot a bit prettier:

现在我想使情节更漂亮一点:

  1. Make the x-axis show length every 5000 units (instead of every 20000)
  2. 使x轴显示每5000个单位的长度(而不是每20000个)

  3. Add x-values on top of the three peaks (approx 3000,5000 and 35000).
  4. 在三个峰顶(约3000,5000和35000)上添加x值。

How can I do that?

我怎样才能做到这一点?

update in response to James: 如何使ggplot2图更漂亮?

更新以回应詹姆斯:

2 个解决方案

#1


11  

How about:

(first create a reproducible example)

(首先创建一个可重现的例子)

set.seed(1001)
lengths <- data.frame(length=c(rgamma(1000,shape=10,scale=500),
                    10000+rgamma(1000,shape=5,scale=700),
                    rnorm(500,mean=30000,sd=2000)),
                  library=factor(rep(2:1,c(2000,500))))

(cute stuff to find peak locations and heights)

(可爱的东西找到高峰位置和高度)

peakfun <- function(x) {
  d <- density(x$length)
  peaks <- which(diff(sign(diff(d$y)))==-2)
  data.frame(x=d$x[peaks],y=d$y[peaks])
}

peakdat <- ddply(lengths,.(library),peakfun)
peakdat <- peakdat[-1,] ## drop spurious peak

(draw the plot)

(画出情节)

library(ggplot2)
ggplot(lengths, aes(length, fill = library)) +
  geom_density(alpha = 0.2) +
  scale_x_continuous(limits = c(0,60000),
                     breaks = seq(0,60000,by=5000))+
  geom_text(data=peakdat,aes(x=x,y=y,label=round(x)),vjust=1)

you probably want to tweak the vertical height of the labels a little

你可能想略微调整标签的垂直高度

#2


6  

1: + scale_x_continuous(breaks=rep(5000,12)).

1:+ scale_x_continuous(breaks = rep(5000,12))。

You could also put the xlim declaration in here, using limits, eg,

您也可以使用限制将xlim声明放在这里,例如,

+ scale_x_continuous(breaks=rep(5000,12),limits=c(0,60000))

2: For the labels you could use annotate() or geom_text(). See this post for examples. You would have to calculate the values yourself for this though.

2:对于标签,您可以使用annotate()或geom_text()。有关示例,请参阅此帖子。你必须自己计算这个值。

#1


11  

How about:

(first create a reproducible example)

(首先创建一个可重现的例子)

set.seed(1001)
lengths <- data.frame(length=c(rgamma(1000,shape=10,scale=500),
                    10000+rgamma(1000,shape=5,scale=700),
                    rnorm(500,mean=30000,sd=2000)),
                  library=factor(rep(2:1,c(2000,500))))

(cute stuff to find peak locations and heights)

(可爱的东西找到高峰位置和高度)

peakfun <- function(x) {
  d <- density(x$length)
  peaks <- which(diff(sign(diff(d$y)))==-2)
  data.frame(x=d$x[peaks],y=d$y[peaks])
}

peakdat <- ddply(lengths,.(library),peakfun)
peakdat <- peakdat[-1,] ## drop spurious peak

(draw the plot)

(画出情节)

library(ggplot2)
ggplot(lengths, aes(length, fill = library)) +
  geom_density(alpha = 0.2) +
  scale_x_continuous(limits = c(0,60000),
                     breaks = seq(0,60000,by=5000))+
  geom_text(data=peakdat,aes(x=x,y=y,label=round(x)),vjust=1)

you probably want to tweak the vertical height of the labels a little

你可能想略微调整标签的垂直高度

#2


6  

1: + scale_x_continuous(breaks=rep(5000,12)).

1:+ scale_x_continuous(breaks = rep(5000,12))。

You could also put the xlim declaration in here, using limits, eg,

您也可以使用限制将xlim声明放在这里,例如,

+ scale_x_continuous(breaks=rep(5000,12),limits=c(0,60000))

2: For the labels you could use annotate() or geom_text(). See this post for examples. You would have to calculate the values yourself for this though.

2:对于标签,您可以使用annotate()或geom_text()。有关示例,请参阅此帖子。你必须自己计算这个值。