0

I hope I have not overlooked an answer to this question: I want to make with ggplot a histogramm of only a fraction of the total data. Here's my example:

df<-iris
ggplot(data=df, aes(x=Sepal.Length, y=..density..*100)) +
geom_bar(binwidth=0.1) +
ylab("percent")

This gives a histogramm of all lines.

Now I want to limit the data passed to the plot (for instance) to a Petal.Width of 0.2. Thus the histogram I wish for, only represents the ratio "count Petal.Width=0.2 divided by total count".

Thanks for helping a ggplot-rookie!! With base plot I managed to work around, but I failed here..

tonytonov
  • 25,060
  • 16
  • 82
  • 98

1 Answers1

0

I think what you want to do is to subset the data you're calling in the plot:

ggplot(data=df[df$Petal.Width == 0.2,], aes(x=Sepal.Length, y=..density..*100)) +
    geom_bar(binwidth=0.1) +
    ylab("percent")

Some other ways to subset data using ggplot are described in this post: Subset and ggplot2

Community
  • 1
  • 1
tsurudak
  • 602
  • 7
  • 14