【问题标题】:Caption with multiple text and color带有多个文本和颜色的标题
【发布时间】:2020-02-28 16:43:55
【问题描述】:

我正在 ggplot 中为我的情节添加标题,没有问题。我可以改变位置和单一的颜色和文字。

我想做的是添加一个包含多个文本的标题,每个文本(单词)都是一个单独的颜色。

library(ggplot2)

ggplot(xdata, aes(x = Days, y= maxGenOutletTemp)) + 
geom_point(shape = 18, color = "#8181F7")+
geom_hline(yintercept = `1yrWTGbase`$Gen.Cool.1YRBase, linetype="solid", color = "#04B431", size = 1)+
geom_smooth(method = lm, linetype = "dashed", color = "red", fullrange = TRUE)+
geom_smooth(method = "auto",se = F)+

scale_x_datetime(date_breaks = "1 week", date_minor_breaks = "1 day",
           date_labels = "%m/%d")+ 
scale_y_continuous(limits=c(min(xdata$maxGenOutletTemp), 85))+

theme_light()+

labs(title = "WTG Generator Coolant.Outlet last 90 days  vs  1yr Wtg.Baseline",
   subtitle = "Last 90 Days",
   caption = "Green.Line -> 1yr WTG.baseline  //  Red.Line -> Trend line  //  Blue.Line -> Daily Moving Trend")+

theme(
plot.title = element_text(hjust = 0.5, size = 14),    # Center title position and size
plot.subtitle = element_text(hjust = 0.5),            # Center subtitle
plot.caption = element_text(hjust = 0, face = "bold"), # move caption to the left
)

我想要一个标题如下:

1yr WTG.baseline(绿色文字),趋势线(红色文字),每日移动趋势(蓝色文字)

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    ggtext 包(我认为目前仅在 Github 上可用)是可能的。它允许使用 HTML 作为标题。

    library(ggplot2)
    library(ggtext)
    
    mycaption <- '<span style="color:red;">RED</span> // <span style="color:blue;">BLUE</span>'
    
    ggplot(iris) + 
      aes(Sepal.Length, Sepal.Width) + 
      labs(caption = mycaption) + 
      theme(plot.caption = element_markdown(hjust = 0, size = 18))
    

    【讨论】:

    • 您可以在这里使用 HTML 在标题中添加图片吗?
    • @MokeEire 我不知道。试试:)
    • 我的基本尝试是将'&lt;img src=image.png&gt;' 添加到mycaptiongrid.Call - Viewport has zero deminsion(s) 中出现错误?我的 HTML 知识不是很好,所以在那里学习一点可能对我有帮助
    • @MokeEire image.png 周围的引号不见了。这有效:mycaption &lt;- '&lt;img src="https://www.r-project.org/logo/Rlogo.png" width="30" height="30"/&gt;'。但是我不知道你是否可以从你的电脑中添加一张图片,我没有尝试。
    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多