How to make overlaid line distributions with and without using ggplot 2 in R?

r
datavisualization
ggplot2

#1

Hi,

I have a data frame in which one column has the count of some quantity and one column has binary values(say 0/1). I want to plot the distribution of the count for both the categories(0/1) on the same plot.
How can I do this task with and without using ggplot2 package.

Thanks.


#2

Here is an example, not using ggplot:

####Create a dataframe with 2 columns: count and category
df=data.frame(count=c(7,2,4,2,6,9,11,10,3,6,3,8,11,10,6,7),category=c(1,0,1,1,0,0,1,1,1,0,0,1,0,1,0,1))
####Plot the data for category 1.
plot(df$count[df$category==1],type=“l”,col=“red”,ylab=“Counts”,main=“Overlaid Plot with 2 categories”)
####On the same plot, add line for category 0
lines(df$count[df$category==0],col=“blue”)

####Add a legend
legend(“topleft”, legend = c(“Category 1”, “Category 0”), col = c(“red”,“blue”), pch = 1, lty = 1)

Hope this helps…