简体   繁体   English

在 ggplot2 错误中添加区域外的文本 plot

[英]Add text outside area plot in ggplot2 error

I'm trying to add text outside area plot like this我正在尝试像这样在 plot 区域之外添加文本

在此处输入图像描述

I tried我试过了

ggplot(data=df, aes(y=Latitude, x=Longitude)) +
    geom_raster(aes(fill=PM10)) +
    scale_y_continuous(breaks = c(-10,-5,0,5), labels = c("10S","5S","0","5N"),
                       expand = c(0,0)) + 
    scale_x_continuous(breaks =c(100,120,140),labels = c("100E","120E","140E"),
                       expand = c(0,0)) +
    ggtitle(expression("Konsentrasi Aerosol PM"[10])) +
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_fill_gradientn(parse(text="PM10 (μg/m^3)"),colours=warna) +
    geom_path(data = shapefile_df, aes(x = long, y = lat, group = group),
              color = 'black', fill= "gray") +
    annotate("text", x = 98.5, y=6.6, label = "2020-04-01  12:00 WITA") +
    annotate("text", x = 98.5, y=-11.6, label = "Waktu Validasi: 2020-04-01  12:00 WITA")

But I got但我得到了

在此处输入图像描述

then i trying to use cowplot然后我尝试使用cowplot

kiri_atas <- as.data.frame("2020-04-01  12:00 WITA")
ann <- ggplot(kiri_atas, aes(x = 0, y = 0, label =kiri_atas)) +
    geom_text() +
    theme_void()

  plot_grid(ann, a, ncol = 1, rel_heights = c(.05, 1), align = 'v')

I got我有

在此处输入图像描述

I try to use gtable我尝试使用gtable

a <-  ggplot(data=df, aes(y=Latitude, x=Longitude)) +
    geom_raster(aes(fill=PM10)) +
    scale_y_continuous(breaks = c(-10,-5,0,5), labels = c("10S","5S","0","5N"),
                       expand = c(0,0)) + 
    scale_x_continuous(breaks =c(100,120,140),labels = c("100E","120E","140E"),
                       expand = c(0,0)) +
    ggtitle(expression("Konsentrasi Aerosol PM"[10])) +
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_fill_gradientn(parse(text="PM10 (μg/m^3)"),colours=warna) +
    geom_path(data = shapefile_df, aes(x = long, y = lat, group = group),
              color = 'black', fill= "gray")
a = gtable_add_grob(a, grobTree(textGrob("left", x=0, hjust=0), 
                                textGrob("right", x=1, hjust=1)), 
                    t=1, l=4)

grid.draw(a)

Nothing happens, pls help me to solve this how to add text like first picture in topleft, topright, bottomleft, bottomright?没有任何反应,请帮我解决这个问题如何在左上角、右上角、左下角、右下角添加第一张图片等文本? And get Title in center and bold?并在中心和粗体中获得标题?

Just a quick answer, but you can try adding something like a subtitle and caption, for some of the text.只是一个快速的答案,但您可以尝试为某些文本添加诸如副标题和标题之类的内容。 I got the information from here: https://www.datanovia.com/en/blog/ggplot-title-subtitle-and-caption/我从这里得到信息: https://www.datanovia.com/en/blog/ggplot-title-subtitle-and-caption/

ggplot(data=df, aes(y=Latitude, x=Longitude)) +
    geom_raster(aes(fill=PM10)) +
    scale_y_continuous(breaks = c(-10,-5,0,5), labels = c("10S","5S","0","5N"),
                       expand = c(0,0)) + 
    scale_x_continuous(breaks =c(100,120,140),labels = c("100E","120E","140E"),
                       expand = c(0,0)) +
    theme(plot.title = element_text(hjust = 0.5)) +
    scale_fill_gradientn(parse(text="PM10 (μg/m^3)"),colours=warna) +
    geom_path(data = shapefile_df, aes(x = long, y = lat, group = group),
              color = 'black', fill= "gray") +
    labs(title = "title",
              subtitle = "subtitle",
              caption = "caption")

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

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