首页 > 解决方案 > 具有 2 个不同数值变量的双 Y 轴时间序列:ggplot 的主题()函数不起作用,无法更改图例中文本元素的大小

问题描述

这是我想要做的:我只想更改双 Y 轴时间序列图的图例中标签文本的大小。

我的数据框看起来像这样:

date <- c("2017-05-30", "2017-05-30", "2017-05-30", "2017-05-30", "2017-05-30", "2017-05-31", "2017-05-31", "2017-05-31", "2017-05-31", "2017-05-31", "2017-06-01", "2017-06-01", "2017-06-01", "2017-06-01", "2017-06-01", "2017-06-02", "2017-06-02", "2017-06-02", "2017-06-02", "2017-06-02", "2017-06-03", "2017-06-03", "2017-06-03", "2017-06-03", "2017-06-03")

ttm <- c("sand", "srcw", "wr", "wrcw", NA, "sand", "srcw", "wr", "wrcw", NA, "sand", "srcw", "wr", "wrcw", NA, "sand", "srcw", "wr", "wrcw", NA, "sand", "srcw", "wr", "wrcw", NA)
ttm <- as.factor(ttm)

mean <- c(26.377, 22.994, 6.154, 3.072, NA, 23.650, 24.822, 6.180, 3.012, NA, 25.423, 30.509, 5.765, 2.574, NA, 24.822, 30.494, 5.813, 2.780, NA, 27.168, 29.728, 5.031, 2.102, NA)
mean <- as.numeric(mean)

ptot <- c(NA, NA, NA, NA, 1.8, NA, NA, NA, NA, 8.0, NA, NA, NA, NA, 4.6, NA, NA, NA, NA, 0, NA, NA, NA, NA, 0)
ptot <- as.numeric(ptot)

df<-data.frame(date, ttm, mean, ptot)

df$date <- as.Date(df$date)

df$mean[is.na(df$mean)]<-0
df$ptot[is.na(df$ptot)]<-0

ttm我试图为变量的每个因子绘制一个带有线条的时间序列,并将ptot. 我成功了。但是,我不知道为什么,无论我在theme()函数中做什么,它都不会改变我的情节。

这是我的情节的代码:

{
library(ggplot2)
library(plotly)
library(scales)
library(rstatix)
}

p <- ggplot(df, 
            aes(x = date)) +
  geom_bar(aes(y = ptot,
               fill = "Rainfall"),
           stat = "identity") +
  scale_fill_manual(values = "grey")+
  geom_line(aes(y = mean, linetype = ttm), size = 1)+
  geom_point(aes(y = mean, shape = ttm), size = 3)+
  scale_y_continuous(sec.axis = sec_axis(trans = ~ ., 
                                         name = "VWC (%)"))+
  scale_x_date(date_breaks = "2 weeks",
               date_minor_breaks = "1 week",
               date_labels = "%d %b") + 
  scale_linetype_manual(breaks = c("sand", "srcw", "wr", "wrcw"),
                        values = c("dashed", "dashed", "solid", "solid"),
                        labels = c("S", "RCW/S", "WR", "RCW/WR")) +
  scale_shape_manual(breaks = c("sand", "srcw", "wr", "wrcw"),
                     values = c(1, 17, 1, 17),
                     labels = c("S", "RCW/S", "WR", "RCW/WR")) +
  labs(y = "Total rainfall (mm)", 
       x = "Date",
       linetype = "Treatments",
       shape = "Treatments",
       fill = NULL) +
  theme(legend.text = element_text(size = 100)+
  theme_bw()

p

element_text(size =)无论我在 中指示什么值,或者我试图用函数改变什么,这都是我得到的代码theme

这是我用这段代码得到的图表

标签: rdateggplot2themeslegend

解决方案


推荐阅读