0

我在这里查看了很多答案,但无法解决我的问题 - 所以我想在这里问。我想我需要调用这个因素?我不能让它工作。我正在从 excel csv 文件中导入我的数据。

我希望将图例组织起来 Hypolimnion, Metalimnion (Bottom), Metalimnion (Top), Epilimnion

我也不明白为什么九月没有出现在我的 x 轴上

我只包括我的数据的一个子集

Date              Reservoir          Result
9/24/2019         Epilimnion           0.03
9/24/2019         Metalimnion (Top)    0.06
9/24/2019         Metalimnion (Bottom) 0.9
9/24/2019         Hypolimnion          1.63
10/8/2019         Epilimnion           0.03
10/8/2019         Metalimnion (Top)    0.13
10/8/2019         Metalimnion (Bottom) 0.81
10/8/2019         Hypolimnion          1.45

TP<-read.csv("TP.csv", header=TRUE)
library(ggplot2)
library(scales)
TP$Date <- as.Date(TP$Date, "%m/%d/%Y")
p<-ggplot(TP, aes(x=Date, y=Result, group=Reservoir, color=Reservoir))  + 
theme(plot.title = element_text(hjust = 0.5)) + xlab("") + ylab("Total Phosphorus (mg/L)") +      

geom_point(size = 4) +
theme(axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0)) + 
scale_x_date(breaks = date_breaks("1 month"), labels = date_format("%b %Y")) +    

geom_hline(yintercept      

=    0.1, linetype = 2, colour='turquoise4', size = 1) + 
theme(legend.position="bottom") + theme(legend.title=element_blank())
p<-p+expand_limits(y=0) 
p + scale_y_continuous(expand = c(0, 0.1))

在此处输入图像描述

这是我试图根据下面的出色响应重新创建的代码

setwd("P:/Projects/Coleman Engineering/Products/R work")
TP<-read.csv("TP.csv", header=TRUE)
library(lubridate)
library(ggplot2)
library(scales)
ggplot(TP, aes(x = mdy(Date), y = Result, color = Reservoir))+
geom_point(size = 4)+
ylab("Total Phosphorus (mg/L)") +
scale_x_date(name = "", breaks = date_breaks("1 month"), labels =        

date_format("%b %Y"),  limits = c(mdy("08/30/2019"),mdy("1/30/2020"))) + 
scale_color_discrete(labels = c("Hypolimnion", "Metalimnion (Bottom)",    

"Metalimnion (Top)", "Epilimnion"), breaks = c("Hypolimnion",    

"Metalimnion(Bottom)", "Metalimnion(Top)", "Epilimnion"))+
 geom_hline(yintercept = 0.1, linetype = 2, colour='turquoise4', size = 1)   

+ theme(legend.position="bottom",
axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0),
legend.title=element_blank(),
plot.title = element_text(hjust = 0.5))

在此处输入图像描述

4

1 回答 1

1

您不需要在绘图之前设置因子水平(您可以,但这取决于您),您可以在scale_color_discrete函数中使用参数breakslabels.

对于日期问题,您需要设置一些limits. 这里我使用了 package.json 中的mdy函数lubridate

library(lubridate)
library(ggplot2)
library(scales)
ggplot(df, aes(x = mdy(Date), y = Result, color = Reservoir))+
  geom_point(size = 4)+
  ylab("Total Phosphorus (mg/L)") +
  scale_x_date(name = "", breaks = date_breaks("1 month"), labels = date_format("%b %Y"), 
               limits = c(mdy("08/30/2019"),mdy("1/30/2020"))) + 
  scale_color_discrete(labels = c("Hypolimnion", "Metalimnion (Bottom)", "Metalimnion (Top)", "Epilimnion"),
                       breaks = c("Hypolimnion", "Metalimnion (Bottom)", "Metalimnion (Top)", "Epilimnion"))+
  geom_hline(yintercept =    0.1, linetype = 2, colour='turquoise4', size = 1) + 
  theme(legend.position="bottom",
        axis.text.x = element_text(angle = 45, vjust = 1.0, hjust = 1.0),
        legend.title=element_blank(),
        plot.title = element_text(hjust = 0.5)) 

在此处输入图像描述

它看起来像你想要得到的吗?

顺便说一句,您不必theme每次需要修改情节的一个方面时都调用,您可以将它们全部放在一个调用中。这将使您的代码更易于阅读。

示例数据

structure(list(Date = c("9/24/2019", "9/24/2019", "9/24/2019", 
"9/24/2019", "10/8/2019", "10/8/2019", "10/8/2019", "10/8/2019"
), Reservoir = c("Epilimnion", "Metalimnion (Top)", "Metalimnion (Bottom)", 
"Hypolimnion", "Epilimnion", "Metalimnion (Top)", "Metalimnion (Bottom)", 
"Hypolimnion"), Result = c(0.03, 0.06, 0.9, 1.63, 0.03, 0.13, 
0.81, 1.45)), row.names = c(NA, -8L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x55b57392b350>)
于 2020-01-24T00:41:09.613 回答