Histogram: how to "choose" one variable from a data.frame?

Hi

I have a data frame with two variables. I want to make a histogram with the results from one variable only. E.g., as you can see in my data below, I have two variables in the "treatment" group, "start" and "end". What do I need to write to only pick the the values of the "start" category?

Thank you!!

This is my data:

data.frame(
stringsAsFactors = FALSE,
treatment = c("start","start","start",
"start","start","start","start","start","start","start",
"start","start","start","start","start","start",
"start","start","start","start","start","start",
"start","start","start","start","start","start","start",
"start","end","end","end","end","end","end",
"end","end","end","end","end","end","end","end","end",
"end","end","end","end","end","end","end","end",
"end","end","end","end","end","end","end"),
kg = c(45L,56L,35L,47L,46L,49L,
61L,50L,42L,50L,45L,38L,30L,46L,53L,48L,43L,54L,
67L,78L,63L,85L,79L,60L,74L,78L,57L,76L,91L,
77L,53L,65L,37L,58L,43L,54L,75L,54L,37L,56L,
60L,39L,37L,54L,48L,43L,47L,50L,76L,85L,62L,98L,
81L,66L,85L,83L,53L,89L,94L,90L)
)

Here is one method using dplyr and ggplot2.

DF <- data.frame(
  stringsAsFactors = FALSE,
  treatment = c("start","start","start",
                "start","start","start","start","start","start","start",
                "start","start","start","start","start","start",
                "start","start","start","start","start","start",
                "start","start","start","start","start","start","start",
                "start","end","end","end","end","end","end",
                "end","end","end","end","end","end","end","end","end",
                "end","end","end","end","end","end","end","end",
                "end","end","end","end","end","end","end"),
  kg = c(45L,56L,35L,47L,46L,49L,
         61L,50L,42L,50L,45L,38L,30L,46L,53L,48L,43L,54L,
         67L,78L,63L,85L,79L,60L,74L,78L,57L,76L,91L,
         77L,53L,65L,37L,58L,43L,54L,75L,54L,37L,56L,
         60L,39L,37L,54L,48L,43L,47L,50L,76L,85L,62L,98L,
         81L,66L,85L,83L,53L,89L,94L,90L)
)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
DF %>% filter(treatment == "start") %>% 
  ggplot(aes(x = kg)) + geom_histogram(fill = "skyblue", color = "white", bins = 10)

Created on 2022-05-22 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.