4

currently, I'm using ggplot2 to make density plot.

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

using this code, I can get the plot below. enter image description here

However, I can get different plot if I used plot(density()...) function in R. enter image description here

Y value starts from 0.

How can I make the ggplot's plot as like plot(density()...) in R?

user2659088
  • 133
  • 1
  • 7
  • If you are using the `plot.default` function using base coding, you would need to specify the `xlim` argument as `xlim=c(0,##)` where the ## represents the maximum value you want to represent. – ccapizzano May 26 '17 at 14:05

2 Answers2

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"))
Sorif Hossain
  • 1,231
  • 1
  • 11
  • 18
0

ggplot obviously cut off the x-axis at the min and max of the empirical distribution. You can extend the x-axis by adding xlim to the plot but please make sure that the plot does not exceed the theoretical limit of the distribution (in the example below, the theoretical limit is [0, 1], so there is not much reason to show outside the range).

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))
amatsuo_net
  • 2,409
  • 11
  • 20