多色标题与ggplot2为R [英] Multicolor titles with ggplot2 for R

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

问题描述

我试图实现多色文本,如下所示:





其中引用了这一点:

R中的多色文本



'p>这是我从的

  require(ggplot2)
require(grid)
png( file =multicolortitle.png,width = 800,height = 500)
qplot(x = hp,y = mpg,data = mtcars,color = factor(mtcars $ cyl),size = 2)+
scale_colour_manual(values = c(red3,green3,blue3))+
theme_bw()+
opts(title =\\\
)+
opts (1,line),
label = paste(4 cylinder,paste(rep(,spacing * 2),collapse ='')),
gp = gpar(col =red3,fontsize = 16,fontface = (),
grid.text(0.5,unit(1,npc) - unit(1,line),
label = paste(paste(rep(,spacing)) ),
paste(rep(,spacing),collapse ='')),
gp = gpar(col =green3,fontsize = 16) ,fontface =bold))
grid.text(0.5,unit(1,npc) - unit(1,line),
label = paste(paste(rep( ,'*'),
gp = gpar(col =blue3,fontsize = 16,fontface =bold))
grid.text (0.5,单位(1,npc) - 单位(2,行),
label =粘贴(粘贴(rep(,spacing * 0),collapse =''),
) - 马力与每加仑英里数),
gp = gpar(col =black,fontsize = 16,fontface =bold))
dev.off()

下面是结果图:



所以,我的问题是:是否有更优雅的方法来使用它?例如,我希望能够使用 ggsave ,并且为此创建间距是一个高度手动的过程 - 不适用于需要自动生成数百个这种性质的情节。我可以看到在这之上编写了一些函数,但是也许有更好的方法来实现底图绘制函数所使用的方法吗? 解决方案

这是一个更通用的方法,它利用了一些额外的网格功能。它不是特别好抛光,但它可能会给你一些有用的想法:

pre $ library $ grid
library( GGPLOT2)
$ b $碱基< - ggplot(数据= mtcars,AES(MPG,马力,颜色=因子(CYL),大小= 2))+
geom_point()+ theme_bw()+
opts(title =\\\
)+ opts(legend.position =none)

##获得因子水平
levs < - 水平(因子mtcars $ CYL))$ b亿$< - 长度(列弗)

##获取因素绘制颜色
G< - ggplot_build(p)
D< - 独特(g $ data [[1]] [c(color,group)])
cols < - d $ color [order(d $ group)]

# #使用最宽的标签宽度来确定间隔
labs < - paste(levs,cylinder)
xlocs< - unit(0.5,npc)+
1.1 *(seq_len n) - 平均值(seq_len(n)))* max(单位(1,strwidth,实验室))

##绘制设备前10%的标签
pushViewport(视口(y = 0.95,height = 0.1))
grid.text(paste(levs,cylinder),
x = xlocs,y = un它(0.5,lines),
gp = gpar(col = cols,fontface =bold))
grid.text( - 马力与Miles每加仑,
y =单位(-0.5,lines))
upViewport()

##绘制设备底部90%的主要图形
pushViewport(viewport(y = 0.45,height = 0.9))
print(p,newpage = FALSE)
upViewport()


I was trying to implement multicolor texts as shown here:

multicolor text on chart

which referenced this:

multicolor text in R

This is what I came up with (with help from here):

require(ggplot2)
require(grid)
png(file="multicolortitle.png",width=800,height=500)
qplot(x = hp,y = mpg,data = mtcars,color=factor(mtcars$cyl),size=2) +
  scale_colour_manual(values = c("red3","green3","blue3")) + 
  theme_bw() +
  opts(title = " \n ") +
  opts(legend.position = "none") 
spacing  <- 20
grid.text(0.5, unit(1,"npc") - unit(1,"line"), 
          label=paste("4 cylinder,",paste(rep(" ",spacing*2), collapse='')),
          gp=gpar(col="red3", fontsize=16,fontface="bold"))
grid.text(0.5, unit(1,"npc") - unit(1,"line"), 
          label=paste(paste(rep(" ",spacing), collapse=''),"6 cylinder,",
            paste(rep(" ",spacing), collapse='')),
          gp=gpar(col="green3", fontsize=16,fontface="bold"))
grid.text(0.5, unit(1,"npc") - unit(1,"line"), 
          label=paste(paste(rep(" ",spacing*2), collapse=''),"8 cylinder"),
          gp=gpar(col="blue3", fontsize=16,fontface="bold"))
grid.text(0.5, unit(1,"npc") - unit(2,"line"), 
          label=paste(paste(rep(" ",spacing*0), collapse=''),
            "- Horsepower versus Miles per Gallon"),
          gp=gpar(col="black", fontsize=16,fontface="bold"))
dev.off()

Here's the resulting graph:

So, my question: is there a more elegant method to use for this? I'd like to be able to use ggsave for example, and creating the spacing for this is a highly manual process - not suited for scenarios where I need to automatically make hundreds of plots of this nature. I could see writing some functions on top of this, but maybe there's a better way to implement the methods utilized with the base plotting function?

解决方案

Here's a more general approach that takes advantage of a few additional grid functions. It's not particularly well-polished, but it may give you some useful ideas:

library(grid)
library(ggplot2)

p <- ggplot(data=mtcars, aes(mpg,hp,color=factor(cyl),size=2)) + 
       geom_point() + theme_bw() +
       opts(title = " \n ") + opts(legend.position="none")

## Get factor levels 
levs <- levels(factor(mtcars$cyl))
n <- length(levs)

## Get factors' plotting colors
g <- ggplot_build(p)
d <- unique(g$data[[1]][c("colour", "group")])
cols <- d$colour[order(d$group)]

## Use widest label's width to determine spacing
labs <- paste(levs, "cylinder")
xlocs <- unit(0.5, "npc") + 
         1.1 * (seq_len(n) - mean(seq_len(n))) * max(unit(1, "strwidth", labs))

## Plot labels in top 10% of device
pushViewport(viewport(y=0.95, height=0.1))
    grid.text(paste(levs, "cylinder"), 
              x = xlocs, y=unit(0.5, "lines"), 
              gp = gpar(col=cols, fontface="bold"))
    grid.text("- Horsepower versus Miles per Gallon", 
              y = unit(-0.5, "lines"))
upViewport()

## Plot main figure in bottom 90% of device
pushViewport(viewport(y=0.45, height=0.9))
    print(p, newpage=FALSE)
upViewport()

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

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