简体   繁体   English

使用新标签在 ggplot 中手动添加 x 轴?

[英]Manual addition of x-axes in ggplot with new labels?

Hopefully this is a pretty simple question.希望这是一个非常简单的问题。 I am trying to manually label the x-axis for a line graph (temperature) and bar graph (river discharge) with a shared x-axis and different y-axes, yet I have been consistently unsuccessful, and I really don't know why.我正在尝试手动 label 线图(温度)和条形图(河流流量)的 x 轴,共享 x 轴和不同的 y 轴,但我一直不成功,我真的不知道为什么。

My first dataset (bar graph) looks like this:我的第一个数据集(条形图)如下所示:

在此处输入图像描述

And my second dataset (line graph) looks like this:我的第二个数据集(折线图)如下所示:

在此处输入图像描述

This is the script I wrote that gives me the following image:这是我编写的脚本,它为我提供了以下图像:

p.dt <- ggplot(discharge, aes(x=date,y=discharge)) +
  geom_bar(stat="identity",width=1) +
  annotate("rect", xmin=1, xmax=152,ymin=0,ymax=850,alpha=.25) +
  geom_line(data = temp.ave, inherit.aes = FALSE, aes(x=num, y=average/0.03, group=1), size=2) +
  scale_y_continuous(sec.axis = sec_axis(~.*0.03, name = "Temperature (C)")) +
  theme_linedraw(base_size = 18) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        strip.text = element_text(face = "bold")) +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  labs(y = "Discharge (cfs)", x = "") 
  #ggtitle("USGS Gauge 09112200 Discharge and HOBO Temp")

print(p.dt)

在此处输入图像描述

Okay.好的。 So, I would like to manually label the x-axis every fourth month (example: 11/1/17 to "Nov 2017"), and the data points are shared between the two datasets.所以,我想每四个月手动 label x 轴(例如:11/1/17 到“2017 年 11 月”),数据点在两个数据集之间共享。 I have been adding a single line of scale_x_discrete after adding the temperature layer as a second axis with no avail:在将温度层添加为第二个轴后,我一直在添加一行scale_x_discrete但无济于事:

p.dt <- ggplot(discharge, aes(x=date,y=discharge)) +
  geom_bar(stat="identity",width=1) +
  annotate("rect", xmin=1, xmax=152,ymin=0,ymax=850,alpha=.25) +
  geom_line(data = temp.ave, inherit.aes = FALSE, aes(x=num, y=average/0.03, group=1), size=2) +
  scale_y_continuous(sec.axis = sec_axis(~.*0.03, name = "Temperature (C)")) +
  scale_x_discrete(labels = c("11/1/17" = "Nov 2017", "3/1/18" = "Mar 2018", "7/1/18" = "Jul 2018")) +
  theme_linedraw(base_size = 18) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        strip.text = element_text(face = "bold")) +
  theme(axis.text.x = element_blank(), axis.ticks = element_blank()) +
  labs(y = "Discharge (cfs)", x = "") 
  #ggtitle("USGS Gauge 09112200 Discharge and HOBO Temp")

print(p.dt)

I imagine this has something to do with my two y-axes and two separate datasets.我想这与我的两个 y 轴和两个单独的数据集有关。 Thoughts?想法?

Thank you in advance,先感谢您,

J Ĵ

You could perhaps have the column date as Posixct and use:您也许可以将日期列为 Posixct 并使用:

scale_x_datetime(labels = date_format("%m-%Y"), breaks = '4 months') +

Another option, following the examples here , you can replace scale_x_discrete with另一种选择,按照此处的示例,您可以将scale_x_discrete替换为

scale_x_date(date_breaks = "4 month", date_labels = "%m-%Y")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM