与R的多彩多姿的标题 [英] Multicolored title with R

查看:144
本文介绍了与R的多彩多姿的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为标题中的某些字词添加颜色到我的图表中。我已经能够



此示例使用 ggplot2 创建条形图以及 grid gridExtra 作为标题功能,但我愿意使用任何解决方案(最好使用 ggplot2 来创建图表本身),它可以为我提供引号中的文本以匹配它们各自的条形图颜色。



对此的任何其他解决方案网站还没有能够解决这个难题,但我很想从R内找到解决方案。



感谢您的帮助!

解决方案

我以太老实的方式编写了标签,第一个 grob 的宽度决定第二个grob的 x 等等,我用 grobTree()来对它们进行分组,因为 gTree 没有自己的大小信息,我给 arrangeGrob()一个参数 padding 保持 gTree 的空间。

  library(grid);库(gridExtra);库(ggplot2)

df< - data.frame(name = c(Rachel,Peter,Gabriel,Bradley),age = c(23,35,12 ,3))
fake_bar_charts< - ggplot(df,aes(x = name,y = age))+
geom_bar(stat =identity,fill = c(rep(gray50, 3),red))+ coord_flip()

grobs< - grobTree(
gp = gpar(fontsize = 14,fontface =bold),
textGrob (label =我花更多时间在'',name =title1,
x = unit(0.2,lines),y = unit(1.4,lines),
hjust = 0 ,vjust = 0),
textGrob(label =Rachel,name =title2,
x = grobWidth(title1)+ unit(0.2,lines),y = unit(1.4 ,lines),
hjust = 0,vjust = 0,gp = gpar(col =red)),
textGrob(label ='than,name =title3,
x = grobWidth(title1)+ grobWidth(title2)+单位(0.2,lines),y =单位(1.4,lines),
hjust = 0,vjust = 0) ,
textGrob(label =with',name =title4,
x = u nit(0.2,lines),y = unit(0.1,lines),
hjust = 0,vjust = 0),
textGrob(label =other family members,name = title5,
x = grobWidth(title4)+单位(0.2,lines),y =单位(0.1,lines),
hjust = 0,vjust = 0,gp = gpar (col =gray50)),
textgrob(label ='。,name =title6,
x = grobWidth(title4)+ grobWidth(title5)+ unit ,lines),y = unit(0.1,lines),
hjust = 0,vjust = 0)


gg < - arrangeGrob(fake_bar_charts, top = grobs,padding = unit(2.6,line))
grid.newpage()
grid.draw(gg)


I'd like to add colors to certain words in titles to my graphs. I've been able to find some precedent here. Specifically, I'd like the text that's wrapped in apostrophes (in the output, below) to correspond to the color of their respective bar charts.

Here's how far I've gotten with titles in R before having to export a PDF to Adobe Illustrator or other program.

name <- c("Peter", "Gabriel", "Rachel", "Bradley")
age <- c(34, 13, 28, 0.9)
fake_graph <- family[order(family$age, decreasing = F), ]
fake_graph <- within(fake_graph, {
    bar_color = ifelse(fake_graph$name == "Rachel", "blue", "gray")
})

# Plot creation
library(ggplot2)
fake_bar_charts <- ggplot() +
  geom_bar(
    data = fake_graph,
    position = "identity",
    stat = "identity",
    width = 0.75,
    fill = fake_graph$bar_color,
    aes(x = name, y = age)
    ) +
  scale_x_discrete(limits = fake_graph$name) +
  scale_y_continuous(expand = c(0, 0)) +
  coord_flip() +
  theme_minimal()
family <- data.frame(name, age)

# Add title
library(grid)
library(gridExtra)
grid_title <- textGrob(
  label = "I spend more time with 'Rachel' than\nwith 'other family members.'",
  x = unit(0.2, "lines"),
  y = unit(0.1, "lines"),
  hjust = 0, vjust = 0,
  gp = gpar(fontsize = 14, fontface = "bold")
)
gg <- arrangeGrob(fake_bar_charts, top = grid_title)
grid.arrange(gg)

Output:

This example uses ggplot2 to create bar charts as well as grid and gridExtra for the title functionality, but I'd be willing to work with any solution (preferably with ggplot2 to create the graph itself) that could provide me with the text in quotes to match their respective bar chart colors.

Any other solutions on this site haven't been able to solve this puzzle, but I would love to find a solution for this from within R.

Thank you for any help!

解决方案

I wrote the label with too honest way. First grob's width decides second grob's x, and so on. I used grobTree() to group them. Because gTree doesn't have own size information, I gave arrangeGrob() an argument padding to keep gTree's space.

library(grid); library(gridExtra); library(ggplot2)

df <- data.frame(name = c("Rachel", "Peter", "Gabriel","Bradley"), age = c(23, 35, 12, 3))
fake_bar_charts <- ggplot(df, aes(x=name, y=age)) + 
  geom_bar(stat="identity", fill = c(rep("gray50",3), "red")) + coord_flip()

grobs <- grobTree(
  gp = gpar(fontsize = 14, fontface = "bold"), 
  textGrob(label = "I spend more time with '", name = "title1",
           x = unit(0.2, "lines"), y = unit(1.4, "lines"), 
           hjust = 0, vjust = 0),
  textGrob(label = "Rachel", name = "title2",
           x = grobWidth("title1") + unit(0.2, "lines"), y = unit(1.4, "lines"),
           hjust = 0, vjust = 0, gp = gpar(col = "red")),
  textGrob(label = "' than", name = "title3",
           x = grobWidth("title1") + grobWidth("title2") + unit(0.2, "lines"), y = unit(1.4, "lines"),
           hjust = 0, vjust = 0),
  textGrob(label = "with '", name = "title4",
           x = unit(0.2, "lines"), y = unit(0.1, "lines"),
           hjust = 0, vjust = 0),
  textGrob(label = "other family members", name = "title5",
           x = grobWidth("title4") + unit(0.2, "lines"), y = unit(0.1, "lines"),
           hjust = 0, vjust = 0, gp = gpar(col = "gray50")),
  textGrob(label = "'.", name = "title6",
           x = grobWidth("title4") + grobWidth("title5") + unit(0.2, "lines"), y = unit(0.1, "lines"),
           hjust = 0, vjust = 0)
)

gg <- arrangeGrob(fake_bar_charts, top=grobs, padding = unit(2.6, "line"))
grid.newpage()
grid.draw(gg)

这篇关于与R的多彩多姿的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆