r:在 ggplot 图形中插入 ggtexttable()
r: Inserting ggtexttable() inside a ggplot graph
我正在尝试插入一个 table,它是我使用 ggpubr 包中的 ggtexttable() 函数创建的,位于我的 ggplot 图的绘图边界内。但是,我不断收到此错误:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class "c("gg", "ggplot")" to a data.frame
我不明白为什么会出现错误,但我感觉这与我的 X 轴上有日期有关?对于解决此问题的任何反馈,我将不胜感激!谢谢!
数据:
HUC_df1 <- structure(list(charnam = c("Total dissolved solids", "Total dissolved solids",
"Total dissolved solids"), stdate = structure(c(11297, 11296,
11298), class = "Date"), val = c(439, 437, 510), HUC14 = c("HUC02030104020030",
"HUC02030104020030", "HUC02030104020030")), .Names = c("charnam",
"stdate", "val", "HUC14"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
HUC1_count<-structure(list(year = "2000", n_greater = 1L, percentage = 33.33,
n = 3L), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-1L), .Names = c("year", "n_greater", "percentage", "n"))
代码:
library(ggpubr)
library(ggplot2)
theme_graphs<- theme_linedraw()+
theme(plot.title=element_text(size=15, face="bold",vjust=0.5,hjust = 0.5),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
plot.background = element_blank(),
panel.background = element_blank(),
legend.position = c(0.5, 0.2),
legend.background = element_blank(),
legend.text=element_text(size=10, face="bold"))
HUC1_table<-ggtexttable(HUC1_count,
theme = ttheme("classic"),rows=NULL,
cols=c("Year","Number of Samples\n>500",
"Percent of Samples\n>500","Total Samples"))
HUC1<-ggplot(data = HUC_df1, aes(x =stdate, y = val)) +
geom_point()+
geom_hline(aes(yintercept = 500,color="red"),size=1.3)+
scale_y_continuous(expand = c(0, 0), limits = c(0))+
coord_cartesian(ylim = c(0, 700))+
scale_x_date(date_labels ="%b%Y")+
ggtitle("Elizabeth R (below Elizabeth CORP BDY) (HUC02030104020030)\nTDS Concentration (mg/L);1997-2018") +
xlab("Year") + ylab(" TDS Concentration (mg/L)")+
scale_color_manual("",
values = c("red"),
labels=c("Freshwater Aquatic Life Criteria for TDS = 500 mg/L"))+
theme_graphs+
theme(legend.position =c(0.5, -0.098))
HUC1<-HUC1+annotation_custom(tableGrob(HUC1_table), xmin=1.5,
xmax=1.8,
ymin=200, ymax=300)
我看到这里有两个问题。首先,tableGrob
是一个从 data.frame 创建 grob 的函数。但是您已经创建了 table 是否不需要该功能。但是 ggtexttable
returns 一个 ggplot 对象,但是你需要一个 grob,所以你需要使用 ggplotGrob
将那个 ggplot 对象变成你可以与 annotation_custom
一起使用的东西。
第二个问题是您指定的 x 值范围。由于您的数据被格式化为 Date 向量,因此这些值存储为自 1970-01-01 以来的天数,因此 1.5 和 1.8 的值超出了您实际绘制的范围。您可以使用
查看您的实际范围
range(as.numeric((HUC_df1$stdate)))
# [1] 11296 11298
所以解决这两个问题,你想要的例子是
HUC1+annotation_custom(ggplotGrob(HUC1_table), xmin=11296,
xmax=11298,
ymin=200, ymax=300)
我正在尝试插入一个 table,它是我使用 ggpubr 包中的 ggtexttable() 函数创建的,位于我的 ggplot 图的绘图边界内。但是,我不断收到此错误:
Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) :
cannot coerce class "c("gg", "ggplot")" to a data.frame
我不明白为什么会出现错误,但我感觉这与我的 X 轴上有日期有关?对于解决此问题的任何反馈,我将不胜感激!谢谢!
数据:
HUC_df1 <- structure(list(charnam = c("Total dissolved solids", "Total dissolved solids",
"Total dissolved solids"), stdate = structure(c(11297, 11296,
11298), class = "Date"), val = c(439, 437, 510), HUC14 = c("HUC02030104020030",
"HUC02030104020030", "HUC02030104020030")), .Names = c("charnam",
"stdate", "val", "HUC14"), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
HUC1_count<-structure(list(year = "2000", n_greater = 1L, percentage = 33.33,
n = 3L), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-1L), .Names = c("year", "n_greater", "percentage", "n"))
代码:
library(ggpubr)
library(ggplot2)
theme_graphs<- theme_linedraw()+
theme(plot.title=element_text(size=15, face="bold",vjust=0.5,hjust = 0.5),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
plot.background = element_blank(),
panel.background = element_blank(),
legend.position = c(0.5, 0.2),
legend.background = element_blank(),
legend.text=element_text(size=10, face="bold"))
HUC1_table<-ggtexttable(HUC1_count,
theme = ttheme("classic"),rows=NULL,
cols=c("Year","Number of Samples\n>500",
"Percent of Samples\n>500","Total Samples"))
HUC1<-ggplot(data = HUC_df1, aes(x =stdate, y = val)) +
geom_point()+
geom_hline(aes(yintercept = 500,color="red"),size=1.3)+
scale_y_continuous(expand = c(0, 0), limits = c(0))+
coord_cartesian(ylim = c(0, 700))+
scale_x_date(date_labels ="%b%Y")+
ggtitle("Elizabeth R (below Elizabeth CORP BDY) (HUC02030104020030)\nTDS Concentration (mg/L);1997-2018") +
xlab("Year") + ylab(" TDS Concentration (mg/L)")+
scale_color_manual("",
values = c("red"),
labels=c("Freshwater Aquatic Life Criteria for TDS = 500 mg/L"))+
theme_graphs+
theme(legend.position =c(0.5, -0.098))
HUC1<-HUC1+annotation_custom(tableGrob(HUC1_table), xmin=1.5,
xmax=1.8,
ymin=200, ymax=300)
我看到这里有两个问题。首先,tableGrob
是一个从 data.frame 创建 grob 的函数。但是您已经创建了 table 是否不需要该功能。但是 ggtexttable
returns 一个 ggplot 对象,但是你需要一个 grob,所以你需要使用 ggplotGrob
将那个 ggplot 对象变成你可以与 annotation_custom
一起使用的东西。
第二个问题是您指定的 x 值范围。由于您的数据被格式化为 Date 向量,因此这些值存储为自 1970-01-01 以来的天数,因此 1.5 和 1.8 的值超出了您实际绘制的范围。您可以使用
查看您的实际范围range(as.numeric((HUC_df1$stdate)))
# [1] 11296 11298
所以解决这两个问题,你想要的例子是
HUC1+annotation_custom(ggplotGrob(HUC1_table), xmin=11296,
xmax=11298,
ymin=200, ymax=300)