【问题标题】:Add text outside area plot in ggplot2 error在 ggplot2 错误中添加区域图外的文本
【发布时间】:2020-04-08 14:52:59
【问题描述】:

我正在尝试像这样在区域图之外添加文本

我试过了

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")

但我得到了

然后我尝试使用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')

我明白了

我尝试使用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)

什么都没有发生,请帮我解决这个问题,如何在左上角、右上角、左下角、右下角添加第一张图片之类的文字?并让标题居中并加粗?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    只是一个简单的答案,但您可以尝试为某些文本添加诸如副标题和标题之类的内容。我从这里得到信息: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")
    

    【讨论】:

    • 不能加2个字幕2个字幕?我试过:a + labs(title = expression("Konsentrasi Aerosol PM"[10]), subtitle = "INDONESIA", caption = "Sumber Data: ECMWF") + theme (plot.title = element_text(hjust = 0.5,size=14 ,face = "bold"), plot.subtitle = element_text(hjust = 1, size=10), plot.caption = element_text(hjust = 1, size=10)) a + labs(subtitle = waktu[i], caption = validasi) + theme (plot.subtitle = element_text(hjust = 0, size=10), plot.caption = element_text(hjust = 0, size=10)) 但它只显示 1 个字幕和标题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2014-11-12
    • 2015-12-16
    • 2023-03-22
    • 1970-01-01
    • 2015-01-24
    相关资源
    最近更新 更多