Shalen
Shalen

Reputation: 95

change of histogram to line plot by ggplot

I need to plot the geom_histogram but as the plots overlap, I want to plot it linear instead of bars, i.e, the x=hp, y=count (percent). Can anybody please help me find how to do it.

library(ggplot2)
library(scales)

ggplot(mtcars, aes(x=hp)) + 
  geom_histogram(binwidth=5)

I have done it by qplot but I need to do it in ggplot and the percent.

  qplot(hp, data = mtcars, geom = "freqpoly", binwidth = 10)

Thanks

Upvotes: 0

Views: 658

Answers (1)

Ben
Ben

Reputation: 30474

You can use geom_freqpoly:

library(ggplot2)
library(scales)

ggplot(mtcars, aes(x=hp, y=..count../sum(..count..))) + 
  geom_freqpoly(binwidth=5) +
  scale_y_continuous(labels = percent_format()) +
  ylab("Percent")

geom_freqpoly plot

Upvotes: 1

Related Questions