R 在ggplot中使用次轴时图例不可见

R 在ggplot中使用次轴时图例不可见,r,ggplot2,dplyr,R,Ggplot2,Dplyr,我有一个3列的数据框,我用ggplot绘制。我需要一个主轴和次轴,因为第二列中的数字比第一列中的数字大得多 df_q # A tibble: 205 x 3 Days_From_First_Use Average_Response N_subjects <drtn> <dbl> <int> 1 0 days 4.96 37

我有一个3列的数据框,我用ggplot绘制。我需要一个主轴和次轴,因为第二列中的数字比第一列中的数字大得多

 df_q
# A tibble: 205 x 3
   Days_From_First_Use Average_Response N_subjects
   <drtn>                         <dbl>      <int>
 1 0 days                          4.96         37
 2 1 days                          4.24         33
 3 2 days                          4.12         31
 4 3 days                          3.90         33
 5 4 days                          4.48         30
 6 5 days                          4.06         29
 7 6 days                          3.69         25
 8 7 days                          4.41         26
 9 8 days                          4.97         25
10 9 days                          4.54         26
# ... with 195 more rows
df_q
#A tibble:205 x 3
从第一次开始的天数使用受试者的平均反应
10天4.96 37
2 1天4.24 33
3 2天4.12 31
4 3天3.90 33
5 4天4.48 30
6 5天4.06 29
7.6天3.69 25
8.7天4.41 26
9.8天4.97 25
10 9天4.54 26
# ... 还有195行
I根据第1列(x轴)绘制第2列(主y轴)和第3列(次y轴),如下所示:

beta <- 1 / round ( max(df_q["N_subjects"]) / 10, 0)

plt <- df_q %>% 
         ggplot(aes(x = as.numeric(Days_From_First_Use), y = Average_Response)) +
         geom_line(linetype = 1, color = "red4", size = 1) +
         geom_line(aes(y = beta * N_subjects), linetype = 12, color = "forestgreen", size = 1) +
         scale_y_continuous( "Average Response Across All Subjects", 
                             limits = c(0, 10), breaks = c(1, 3, 4, 6, 7, 9),
                             sec.axis = sec_axis(~ ./ beta, name = "Number of Responses")) +
         labs(title = "Average Score and Number of Subjects vs Day From First Use", 
              x = "Days From First Use") +
         theme(plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
               axis.title.x = element_text(size = 14, face = "bold"),
               axis.title.y = element_text(size = 14, face = "bold"), 
               legend.title = element_text(size = 14, face = "bold"),
               legend.position = "right")      

# Need to show(plt) to make it visible in RStudio
show(plt)

beta尝试在
aes()中传递
color
linetype
语句:

库(tidyverse)
#代码
贝塔%
变异(从第一次使用开始的天数=解析天数(从第一次使用开始的天数))%>%
ggplot(aes(x=as.numeric(从首次使用开始算起的天数),y=Average响应))+
几何图形线(尺寸=1,aes(颜色=平均响应,线型=平均响应)+
geom_线(aes(y=beta*N_受试者,颜色=”反应次数',
线型='响应数',大小=1)+
连续量表(“所有受试者的平均反应”,
极限=c(0,10),断裂=c(1,3,4,6,7,9),
秒轴=秒轴(~./β,name=“响应数”))+
实验室(title=“平均分数和受试者人数与首次使用后的天数之比”,
x=“首次使用后的天数”)+
主题(plot.title=element\u text(size=16,face=“bold”,hjust=0.5),
axis.title.x=元素\文本(大小=14,面=“粗体”),
axis.title.y=元素\文本(大小=14,face=“bold”),
legend.title=元素\文本(大小=14,面=“粗体”),
legend.position=“右”)+
比例\颜色\手册(值=c(“红色4”,“绿色”))+
比例\线型\手动(值=c(1,12))+
实验室(颜色='var',线型='var')
输出:

使用的一些数据:

#Data
df_q <- structure(list(Days_From_First_Use = c("0 days", "1 days", "2 days", 
"3 days", "4 days", "5 days", "6 days", "7 days", "8 days", "9 days"
), Average_Response = c(4.96, 4.24, 4.12, 3.9, 4.48, 4.06, 3.69, 
4.41, 4.97, 4.54), N_subjects = c(37L, 33L, 31L, 33L, 30L, 29L, 
25L, 26L, 25L, 26L)), class = "data.frame", row.names = c(NA, 
-10L))
#数据

完美!非常感谢你。
#Data
df_q <- structure(list(Days_From_First_Use = c("0 days", "1 days", "2 days", 
"3 days", "4 days", "5 days", "6 days", "7 days", "8 days", "9 days"
), Average_Response = c(4.96, 4.24, 4.12, 3.9, 4.48, 4.06, 3.69, 
4.41, 4.97, 4.54), N_subjects = c(37L, 33L, 31L, 33L, 30L, 29L, 
25L, 26L, 25L, 26L)), class = "data.frame", row.names = c(NA, 
-10L))