df<-data.frame(location=c(1:5),Jan=c(0.5,0.6,0.5,0.4,0.55),Feb=c(0.4,0.33,0.5,0.33,0.4),Mar=c(0.25,0.2,0.33,0.25,0.35))
location Jan Feb Mar
1 1 0.50 0.40 0.25
2 2 0.60 0.33 0.20
3 3 0.50 0.50 0.33
4 4 0.40 0.33 0.25
5 5 0.55 0.40 0.35
The above data frame is measuring percent completion by location by month. I would like a graph illustrating change over time essentially. I've tried something similar to the following, but cannot seem to get it right.
ggplot(df,aes(x=c(Jan,Feb,Mar), y = Jan:Mar, color = location)) geom_point()
CodePudding user response:
There are many, many ways to plot your example dataset. Does this approach solve your problem?
library(tidyverse)
library(scales)
df <- data.frame(location=c(1:5),
Jan=c(0.5,0.6,0.5,0.4,0.55),
Feb=c(0.4,0.33,0.5,0.33,0.4),
Mar=c(0.25,0.2,0.33,0.25,0.35))
df %>%
pivot_longer(-location,
names_to = "month") %>%
mutate(month = factor(month,
levels = month.abb,
ordered = TRUE)) %>%
ggplot(aes(x = month, y = value))
geom_point()
geom_line(aes(group = 1),
lty = 2)
facet_wrap(~location, nrow = 1, labeller = label_both)
scale_y_continuous(labels = percent)
Created on 2022-09-01 by the reprex package (v2.0.1)
More details: your example dataset is currently in the 'wide' format, but if you use the pivot_longer()
function to convert it to 'long' format it makes it easier to work with (https://tidyr.tidyverse.org/reference/pivot_longer.html). Then, use mutate()
to convert the 'type' of each month from character to a factor so that the "months" can be ordered properly (i.e. not alphabetical order (Feb, Jan, Mar) but in the same order as the built-in dataset month.abb
(Jan, Feb, Mar); e.g. Sorting months in R). Then use ggplot2 to add dots and lines, and use facet_wrap()
to create one plot per location. Finally, format the y-axis labels to percentages (i.e. instead of 0.4, 40%).