拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 在标记中断时设定ylim

在标记中断时设定ylim

白鹭 - 2022-03-02 2177 0 0

我有这样的资料:

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"))

我用它制作了这张图:

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"))

在标记中断时设定 ylim

看起来很棒,主要是我想要的。话虽如此,我希望 y 轴从 1 到至少 4,当我尝试用 ylim 做到这一点时,我遇到了麻烦。

当我只使用 ylim() 而不是自定义中断和标签时,作业正常:

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)

在标记中断时设定 ylim

但是当我尝试在我的代码中同时使用这两者时,它就搞砸了:

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)

它告诉我原因(而且它是有道理的):

“'y' 的比例已经存在。为'y' 添加另一个比例,这将替换现有的比例。”

但是我该怎么做呢?更改资料上的标签,同时确保比例足够远?

uj5u.com热心网友回复:

您可以通过以下limits自变量设定限制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))

在标记中断时设定 ylim

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *