I'm a total newbie to R. I'm trying to create a stacked bar chart based on this code:
activity <- c("mean_fairly_active", "mean_fairly_active", "mean_fairly_active",
"mean_fairly_active", "mean_fairly_active", "mean_fairly_active",
"mean_fairly_active", "mean_lightly_active", "mean_lightly_active",
"mean_lightly_active", "mean_lightly_active", "mean_lightly_active",
"mean_lightly_active", "mean_lightly_active", "mean_very_active",
"mean_very_active", "mean_very_active", "mean_very_active",
"mean_very_active", "mean_very_active", "mean_very_active",
"mean_sedentary", "mean_sedentary", "mean_sedentary",
"mean_sedentary", "mean_sedentary", "mean_sedentary",
"mean_sedentary")
minutes <- c("14.3", "13.1", "12.0", "12.1", "15.2", "14.5", "14.0", "197", "190",
"185", "204", "207", "174", "192", "23.0", "20.8", "19.4", "20.1",
"21.9", "20.0", "23.1", "1007", "989", "962", "1000", "964", "990",
"1028")
weekday <- c("Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun", "Mon",
"Tues", "Wed", "Thurs", "Fri", "Sat", "Sun", "Mon", "Tues", "Wed",
"Thurs", "Fri", "Sat", "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri",
"Sat")
merged_means <- data.frame (activity, minutes, weekday)
print(merged_means)
The ggplot I'm running:
ggplot(merged_means, aes(x=weekday, y=minutes, fill=activity)) +
geom_bar(stat = "identity")
The resulting graph shows all the numbers of the y-axis squished on the bottom half of the graph, the days in the wrong chronological order (Fri, Mon, Sat, Sun, etc), and the fill colors in the wrong amounts. Any help is appreciated!
You have a few issues to address:
minutes
to numeric, since it is currently a character vector, and R doesn't know you intend these character strings to be interpreted as numbers.weekday
a factor variable with your chosen ordering.geom_bar(stat = "identity")
is just a long way of writing geom_col()
Putting all these together we might have something like this:
ggplot(merged_means,
aes(x = factor(weekday, unique(weekday)),
y = as.numeric(minutes),
fill = factor(trimws(gsub("mean|_", " ", activity)),
c("very active",
"fairly active",
"lightly active",
"sedentary")))) +
geom_col(color = "gray50") +
scale_fill_brewer(palette = "YlOrRd", direction = -1) +
labs(x = "Weekday", y = "Minutes", fill = "Activity") +
theme_minimal(base_size = 16)