如何让ggplot2绘图更漂亮?

发布于 2024-09-17 01:05:17 字数 461 浏览 3 评论 0原文

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

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

现在我想让绘图更漂亮一点:

  1. 使 x 轴显示长度 5000 个单位(而不是每 20000 个)
  2. 在三个之上添加 x 值 峰值(大约 3000、5000 和 35000)。

我怎样才能做到这一点?

更新 回复 詹姆斯: 替代文本

I have generated the following plot using the R code that follows it:
alt text

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. Add x-values on top of the three
    peaks (approx 3000,5000 and 35000).

How can I do that?

update
in response to James:
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望喜 2024-09-24 01:05:17

怎么样:(

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

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

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

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

(绘制绘图)

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)

您可能想稍微调整标签的垂直高度

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

愁杀 2024-09-24 01:05:17

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

您还可以使用 limitsxlim 声明放在这里,例如

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

2:对于标签,您可以使用 annotate()geom_text()。有关示例,请参阅这篇文章。不过,您必须自己计算这些值。

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

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

+ 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文