I'm trying to plot a graph where number of TRUE statement from a df column.
I have a df that looks like this
Speed Month_1
12 67
12 114
12 155
12 44
13 77
13 165
13 114
13 177
...
And I would like to plot a bargraph where we have x = Speed and y = Number of rows that are above 100 in Month_1 column.
So for X = 12 I would have a bargraph with a Y-value of 2 and for X = 13 I would have a Y-value of 3.
Can I do this directly in ggplot, or do I have to create a new DF first?
CodePudding user response:
Sure, just filter out the values below 100 in the data you pass to ggplot
and do a normal geom_bar
ggplot(df[df$Month_1 >= 100, ], aes(factor(Speed)))
geom_bar(width = 0.5, fill = 'deepskyblue4')
theme_bw(base_size = 16)
labs(x = 'Speed')
If, for some reason, you really need to pass the full data frame without filtering it, you can fill the < 100 values with a fully transparent colour:
ggplot(df, aes(factor(Speed), fill = Month_1 > 100))
geom_bar(width = 0.5)
theme_bw(base_size = 16)
scale_fill_manual(values = c('#00000000', 'deepskyblue4'))
labs(x = 'Speed')
theme(legend.position = 'none')
CodePudding user response:
You can use dplyr
to filter your data frame and then plot it with ggplot
.
library(tidyverse)
df <- tibble(Speed = c(12, 12, 12, 12, 13, 13, 13, 13),
Month_1 = c(67, 114, 155, 44, 77, 165, 114, 177))
df %>% filter(Month_1 > 100) %>%
ggplot(aes(x = Speed)) geom_bar()