Maria Gold Maria Gold - 1 month ago 17
R Question

Conditional histogram in R

I used the following commands to make a histogram of one variable in my dataframe. How Do I make a histogram of the same variable, but only for rows where the value is bigger than 0.8 (if the range of values is 0 to 1.0)?

library('ggplot2')
qplot(df$var,
geom="histogram",
binwidth = 5,
main = "var Distribution",
xlab = "VAR",
fill=I("green"),
col=I("blue"),
alpha=I(.2),
xlim=c(0,1.0))

dmg dmg
Answer

All you need is to subset the frame:

Create your plot with

df[df$var>0.8,]

rather than df

or alternatively, assign it to another variable

df2 = df[df$var>0.8,]

and then create the plot.

Comments