将颜色更改为ggplot(R)中文本中的特定单词

将颜色更改为ggplot(R)中文本中的特定单词,r,ggplot2,R,Ggplot2,我正在尝试使用函数annotate()向ggplot中的文本添加不同的颜色。 结果非常好,但我必须手动定义哪些是正确的y值以正确重叠文本。 我很想知道是否有更好的方法来重叠R中注释中的文本。 谢谢, 顺便说一句,以下是我使用的代码: ex_3_1 %>% ggplot(aes(x = DATE)) + # geometries geom_line(aes(y = if_else(ORIGIN == "ACTUAL" |

我正在尝试使用函数
annotate()
向ggplot中的文本添加不同的颜色。 结果非常好,但我必须手动定义哪些是正确的y值以正确重叠文本。

我很想知道是否有更好的方法来重叠R中注释中的文本。 谢谢,

顺便说一句,以下是我使用的代码:

ex_3_1 %>% 
  ggplot(aes(x = DATE)) +
    # geometries
    geom_line(aes(y = if_else(ORIGIN == "ACTUAL" |
                                (YEAR == 2019 & MONTH == "JUN"), 
                              SALES, NULL)), size = 1) +
    geom_line(aes(y = if_else(ORIGIN == "FORECAST", SALES, NULL)), 
              linetype = "dashed", size = 1) +
    geom_point(aes(y = REL_SALES), size = 3) +
    geom_point(aes(y = if_else(MONTH == "JUL" & YEAR == 2018, SALES, NULL)), 
               shape = 21, fill = "darkorange", size = 3) + 
    geom_point(aes(y = if_else(MONTH == "FEB" & YEAR == 2019, SALES, NULL)), 
               shape = 21, fill = col, size = 3) + 
    geom_text(aes(y = SALES, label = dollar(round(REL_SALES,1), 
                                 suffix = "B", accuracy = 0.1)), 
              vjust = -1.5, hjust = 0.2, size = 3) +
    # annotations
      # square text
    annotate(geom = "rect", xmin = as_date("2019-05-20"), 
             xmax = as_date("2020-01-10"), ymin = 0, ymax = 2.6, 
             alpha = 0.1) + 
    annotate(geom = "text",
             x = as_date("2020-01-01"), y = 1, hjust = 1, vjust = -1,
             label = expression(bold("2019 FORECAST")), 
             col = "gray60", size = 3.25) +
    annotate(geom = "text",
             x = as_date("2020-01-01"), y = 1, hjust = 1, vjust = 1,
             label = paste0("This is provided by ABC\n",
                              "consultants and based on\n",
                              "market data through June.\n",
                              "The forecast assumes no\n",
                              "major market changes.\n"), 
             col = "gray60", size = 3.5) +
      # 2018 notes
    annotate(
      geom = "text", x = as_date("2018-01-10"), y = 3.5, hjust = 0, vjust = 1,
      label = paste0("2018: Jan-Jun was a period of stability, with\n",
                     "fairly steady growth (averaging +3% per\n",
                     "month). There was a nearly 20% decrease\n", 
                     "in July, when Product X was recalled and\n", 
                     "pulled from the market. Total sales remained\n", 
                     "at reduced volume for the rest of the year."),
      col = "gray60", size = 3.5) +   
    annotate(
      geom = "text", x = as_date("2018-01-10"), y = 3.5, hjust = 0, vjust = 1,
      label = expression(bold("2018:")),
      col = "gray60", size = 3.5) +
    annotate(
      geom = "text", x = as_date("2018-01-10"), y = 3.19, hjust = 0, vjust = 1,
      label = expression(phantom("month). There was a ")*
                           "nearly 20% decrease"),
      size = 3.5, col = "darkorange") +
    annotate(
      geom = "text", x = as_date("2018-01-10"), y = 3.03, hjust = 0, vjust = 1,
      label = "in July",
      size = 3.5, col = "darkorange") +
      # 2019 notes
    annotate(
      geom = "text", x = as_date("2019-01-10"), y = 3.5, hjust = 0, vjust = 1,
      label = paste0("2019: The year started at less than $1.6B, but\n",
                     "Increased markedly in February, when a new\n",
                     "study was released. Total sales have increased\n", 
                     "steadly since then and this projected to continue.\n", 
                     "The latest forecast is for $2.4B in monthly sales by\n", 
                     "the end of the year."),
      col = "gray60", size = 3.5) +
    annotate(
      geom = "text", x = as_date("2019-01-10"), y = 3.5, hjust = 0, vjust = 1,
      label = expression(bold("2019:")),
      col = "gray60", size = 3.5) +
    annotate(
      geom = "text", x = as_date("2019-01-10"), y = 3.35, hjust = 0, vjust = 1,
      label = "Increased markedly in February",
      size = 3.5, col = col) +
    # scales
    scale_x_date(date_labels = "%b'%y", date_breaks = "3 month") +
    scale_y_continuous(labels = dollar, breaks = c(seq(0,3.5,0.5)), 
                       limits = c(0, 3.5)) +
    # titles
    labs("Market size over time") +
    ylab("SALES ($USD BILLIONS)") +
    # themes
    theme_void() +
    theme(
      axis.line.x = element_line(color = "gray58"),
      axis.text.y = element_text(size = 11, color = "gray58"),
      axis.title.y = element_text(hjust = 1, color = "gray58"),
      axis.text.x = element_text(size = 9, color = "gray58")
    )

如果您可以重复地共享一点数据,以便我们可以运行您的代码,那么这将是一个更好的问题。不需要是所有内容,只需要足够的行就可以运行代码
dput()
是以可复制/粘贴的方式共享数据的好方法。您还可以查看
?strwrap
,而不是手动换行。您好!谢谢你的回复!这里有一个下载数据的链接:您可能会发现除我之外的其他人愿意下载、导入和实验您的完整数据。但是,如果您以可复制/粘贴的方式共享一点数据,您将更快地获得帮助。有很多提示
dput()
工作得非常好,例如,
dput(droplevels(您的_数据[1:10,])
对前10行进行复制/粘贴。如果您可以找到一个适当的10-20行子集来共享,那么人们可以很容易地帮助您。