I have data like this:
df<-structure(list(PreConfidenceMarginal = c(1, 1, 1, 3, 3, 4, 4,
4, 4, 4, 2, 1, 1, 1, 2, 3, 2, 1), PostConfidenceMarginal = c(2,
2, 2, 4, 4, 4, 4, 4, 5, 4, 3, 3, 1, 3, 4, 3, 3, 3), NoMarginalRepairs = c(TRUE,
TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE)), row.names = c(NA,
-18L), class = c("tbl_df", "tbl", "data.frame"))
and I've made this graph with it:
df%>%pivot_longer(!NoMarginalRepairs, names_to = "Time", values_to = "Value")%>%group_by(Time,NoMarginalRepairs)%>%summarize(Mean=mean(Value),SD=sd(Value, na.rm = TRUE))%>%mutate(Time=as.factor(Time))%>%mutate(Time=fct_recode(Time,"Pre-Workshop"="PreConfidenceMarginal","Post-Workshop"="PostConfidenceMarginal"))%>%mutate(Time = factor(Time, levels = c("Pre-Workshop", "Post-Workshop")))%>%ggplot(aes(x=Time,y=Mean,color=NoMarginalRepairs,group=NoMarginalRepairs)) geom_point() geom_line() labs(color="Has not repaired any marginal eyelid lacerations",title = "Degree of Improvement", subtitle = "Repairing Marginal Lid Laceration" , y= "Confidence Level") scale_y_continuous(breaks = c(1, 2, 3, 4,5),labels = c("1. Poor", "2. Fair", "3. Good", "4. Very Good", "5. Excellent"))
Looks great and is mostly what I want. That being said I'd love the y axis to go from 1 to at least 4, and when I try to do that with ylim I'm having trouble.
When I use only ylim() and not the custom breaks and labels, works fine:
df%>%pivot_longer(!NoMarginalRepairs, names_to = "Time", values_to = "Value")%>%group_by(Time,NoMarginalRepairs)%>%summarize(Mean=mean(Value),SD=sd(Value, na.rm = TRUE))%>%mutate(Time=as.factor(Time))%>%mutate(Time=fct_recode(Time,"Pre-Workshop"="PreConfidenceMarginal","Post-Workshop"="PostConfidenceMarginal"))%>%mutate(Time = factor(Time, levels = c("Pre-Workshop", "Post-Workshop")))%>%ggplot(aes(x=Time,y=Mean,color=NoMarginalRepairs,group=NoMarginalRepairs)) geom_point() geom_line() labs(color="Has not repaired any marginal eyelid lacerations",title = "Degree of Improvement", subtitle = "Repairing Marginal Lid Laceration" , y= "Confidence Level") ylim(1,4)
But when I try to have both in my code like so it messes up:
df%>%filter(!is.na(NoMarginalRepairs))%>%select(PreConfidenceMarginal,PostConfidenceMarginal,NoMarginalRepairs)%>%pivot_longer(!NoMarginalRepairs, names_to = "Time", values_to = "Value")%>%group_by(Time,NoMarginalRepairs)%>%summarize(Mean=mean(Value),SD=sd(Value, na.rm = TRUE))%>%mutate(Time=as.factor(Time))%>%mutate(Time=fct_recode(Time,"Pre-Workshop"="PreConfidenceMarginal","Post-Workshop"="PostConfidenceMarginal"))%>%mutate(Time = factor(Time, levels = c("Pre-Workshop", "Post-Workshop")))%>%ggplot(aes(x=Time,y=Mean,color=NoMarginalRepairs,group=NoMarginalRepairs)) geom_point() geom_line() labs(color="Has not repaired any marginal eyelid lacerations",title = "Degree of Improvement", subtitle = "Repairing Marginal Lid Laceration" , y= "Confidence Level") scale_y_continuous(breaks = c(1, 2, 3, 4,5),labels = c("1. Poor", "2. Fair", "3. Good", "4. Very Good", "5. Excellent")) ylim(1,4)
It tells me why (and it makes sense):
"Scale for 'y' is already present. Adding another scale for 'y', which will replace the existing scale."
But how do I do both? Change the labels on the data and at the same time ensure that the scale goes far enough?
CodePudding user response:
You could set the limits via the limits argument of scale_y_continuous:
library(tidyverse)
df_long <- df %>%
pivot_longer(!NoMarginalRepairs, names_to = "Time", values_to = "Value") %>%
group_by(Time, NoMarginalRepairs) %>%
summarize(Mean = mean(Value), SD = sd(Value, na.rm = TRUE)) %>%
mutate(Time = as.factor(Time)) %>%
mutate(Time = fct_recode(Time, "Pre-Workshop" = "PreConfidenceMarginal", "Post-Workshop" = "PostConfidenceMarginal")) %>%
mutate(Time = factor(Time, levels = c("Pre-Workshop", "Post-Workshop")))
ggplot(df_long, aes(x = Time, y = Mean, color = NoMarginalRepairs, group = NoMarginalRepairs))
geom_point()
geom_line()
labs(color = "Has not repaired any marginal eyelid lacerations",
title = "Degree of Improvement",
subtitle = "Repairing Marginal Lid Laceration",
y = "Confidence Level")
scale_y_continuous(breaks = c(1, 2, 3, 4, 5),
labels = c("1. Poor", "2. Fair", "3. Good", "4. Very Good", "5. Excellent"),
limits = c(1, 4))



