如何在R中使用ggplot2使Y轴从零开始显示数值

4

目前,我正在使用ggplot2制作密度图。

ggplot(data=resultFile,aes(x=V19, colour=V1) ) +
geom_line(stat="density") +
xlab("score") +
ylab("density") +
ggtitle(paste(data_name,protocol,level,sep=" ")) +
theme(legend.title=element_blank(), legend.position=c(0.92,0.9)) +
scale_color_manual(values=c("blue","red"),
                   labels=c("A", "B"))

使用这段代码,我可以得到下面的图表。 enter image description here 然而,如果我在R中使用plot(density()...)函数,我会得到不同的图表。 enter image description here y值从0开始。
如何使ggplot的图表看起来像R中的plot(density()...)?

如果您正在使用基本编码使用plot.default函数,则需要将xlim参数指定为xlim=c(0,##),其中##表示要表示的最大值。 - ccapizzano
2个回答

1
    ggplot(data=resultFile,aes(x=V19, colour=V1) ) +
    ylim(0,range) #you can use this . 
    geom_line(stat="density") +
    xlab("score") +
   ylab("density") +
   ggtitle(paste(data_name,protocol,level,sep=" ")) +
   theme(legend.title=element_blank(), legend.position=c(0.92,0.9)) +
   scale_color_manual(values=c("blue","red"),
   labels=c("A", "B"))

0

ggplot 显然将 x 轴截断在经验分布的最小值和最大值处。您可以通过向图中添加 xlim 来扩展 x 轴,但请确保图形不超过分布的理论极限(在下面的示例中,理论极限为 [0, 1],因此没有太多理由显示范围之外的内容)。

set.seed(1)
temp <- data.frame(x =runif(100)^3)
library(ggplot2)
ggplot(temp, aes(x = x)) + geom_line(stat = "density" + xlim(-.2, 1.2)
plot(density(temp$x))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接