geom_bar()+象形图,如何? [英] geom_bar() + pictograms, how to?

查看:148
本文介绍了geom_bar()+象形图,如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(请参阅帖子底部的更新内容)



首个帖子,2014-07-29 11:43:38



我在已经开始建立一个R功能来生产酒吧与象形图聊天,很容易适应对于ggplot2,











$ < - ggplot(df3,aes(what,units))+
geom_bar(color =black,stat =identity)+
coord_flip()

pattern < - pattern(circleGrob(r = .4,gp = gpar(fill =gray)),
width = .05,height = .05)
registerPatternFill(circles,pattern)
gridsvg(pattern.svg)
print(p)
grid.force()
grid.patternFill(geom_rect.rect,grep = TRUE,group = FALSE ,
label = rep(circles,length(levels(df3 $ what))))
dev.off()


(See bottom of post for updates)

Initial post, 2014-07-29 11:43:38Z

I saw this graphics on the Economist's website and wondered if it's possible to produce a geom_bar() with this kinda illustrative icons imbedded? (dummy data below)

Dummy data,

require(ggplot2)

# Generate data
df3 <- data.frame(units = c(1.3, 1.8, 2.7, 4.2, 4.7, 6.7, 20), 
                   what = c('Wikipedia', 'London Olympic Park', 'Aircraft carrier', 
                            'The Great Pyramid', 'Stonehenge', 'Burj Khalifas', 
                            'Empire State Building'))

# make gs an ordered factor
df3$what <- factor(df3$what, levels = df3$what, ordered = TRUE)

    #plots
    ggplot(df3, aes(what, units)) + geom_bar(fill="white", colour="darkgreen", 
           alpha=0.5, stat="identity") + coord_flip() + scale_x_discrete() + 
           scale_y_continuous(breaks=seq(0, 20, 2)) + theme_bw() + 
           theme(axis.title.x  = element_blank(), axis.title.y  = element_blank())

Update #1, 2014-07-29 15:07:51Z

Apparently Robert Grant has started to build an R function to produce bar-charts with pictograms, it can be found at Github. Thanks to Andrie for that information. I'm currently working to see if Robert's function can do what I am looking for.

Please chime in if you have any advice on how to answer my question using Robert's function.

Update #2, 2014-08-02 12:35:19Z

Here is a simple illustration of how Grant's R-pictogram-function work

# in case you don't alredy have RCurl
# install.packages("RCurl", dependencies = TRUE)
source_github <- function(u) {
  # load package
  require(RCurl)

  # read script lines from website and evaluate
  script <- getURL(u, ssl.verifypeer = FALSE)
  eval(parse(text = script),envir=.GlobalEnv)
}

Got this script form this SO answer

source_github("https://raw.githubusercontent.com/robertgrant/pictogram/master/pictogram.R")

# install.packages("png", dependencies = TRUE)
  require(png)

img <- readPNG(system.file("img", "Rlogo.png", package="png"))
pictogram(icon = img, n = c( 12, 35, 7),
grouplabels=c("12 R logos","35 R logos","7 R logos"))

This gives you this kinda plot

解决方案

gridSVG offers support for svg features unavailable to the R engine, such as fill patterns and arbitrary clipping. This example can easily be adapted for ggplot2,

library(grid)
library(gridSVG)
require(ggplot2)

p <- ggplot(df3, aes(what, units)) + 
  geom_bar(colour="black", stat="identity") +
  coord_flip()

pattern <- pattern(circleGrob(r=.4, gp=gpar(fill="grey")),
                   width=.05, height=.05)
registerPatternFill("circles", pattern)
gridsvg("pattern.svg")
print(p)
grid.force()
grid.patternFill("geom_rect.rect", grep=TRUE, group=FALSE,
                 label=rep("circles", length(levels(df3$what))))
dev.off()

这篇关于geom_bar()+象形图,如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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