- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 the Economist's website 上看到了这个图形想知道是否可以生成一个嵌入这种说明性图标的 geom_bar()
? (下面是虚拟数据)
虚拟数据,
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())
显然Robert Grant已开始构建 R 函数来生成带有象形图的条形图,it can be found at Github 。感谢Andrie以获得该信息。我目前正在研究 Robert 的函数是否可以实现我想要的功能。
如果您对如何使用 Robert's function 回答我的问题有任何建议,请留言。 .
这里是 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)
}
得到这个脚本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 idea 得出的结论。 R logo摘自维基百科。
library(png)
fill_images <- function()
{
l <- list()
for (i in 1:nrow(df3))
{
for (j in 1:floor(df3$units[i]))
{
#seems redundant, but does not work if moved outside of the loop (why?)
img <- readPNG("~/../Rlogo.png")
g <- rasterGrob(img, interpolate=TRUE)
l <- c(l, annotation_custom(g, xmin = i-1/2, xmax = i+1/2, ymin = j-1, ymax = j))
}
}
l
}
p <- ggplot(df3, aes(what, units)) +
geom_bar(fill="white", colour="darkgreen", alpha=0.5, stat="identity") +
coord_flip() +
scale_y_continuous(breaks=seq(0, 20, 2)) +
scale_x_discrete() +
theme_bw() +
theme(axis.title.x = element_blank(), axis.title.y = element_blank()) +
fill_images()
p
我不太确定绘制部分图像的最佳方法是什么。
事实上,这比我想象的要容易。我通过在图像的一部分上绘制白色矩形来剪辑图像。请注意,geom_bar
应位于顶部,以便剪切矩形不会影响它。网格线存在一个小问题(它们部分被这些白色矩形隐藏),所以我必须对它们的位置进行硬编码并手动恢复它们。当然,这不是一个理想的解决方案,但我不知道如何以编程方式检索网格位置。不管怎样,最终的情节完成了任务,而且看起来也很漂亮!
library(png)
fill_images <- function()
{
l <- list()
for (i in 1:nrow(df3))
{
for (j in 1:ceiling(df3$units[i]))
{
img <- readPNG("~/../Rlogo.png")
g <- rasterGrob(img, interpolate=TRUE)
l <- c(l, annotation_custom(g, xmin = i-1/2, xmax = i+1/2, ymin = j-1, ymax = j))
}
}
l
}
clip_images <- function(restore_grid = TRUE)
{
l <- list()
for (i in 1:nrow(df3))
{
l <- c(l, geom_rect(xmin = i-1/2, xmax = i+1/2,
ymin = df3$units[i], ymax = ceiling(df3$units[i]),
colour = "white", fill = "white"))
if (restore_grid && ceiling(df3$units[i]) %in% major_grid)
l <- c(l, geom_segment(x = i-1, xend = i+1,
y = ceiling(df3$units[i]),
yend = ceiling(df3$units[i]),
colour = grid_col, size = grid_size))
}
l
}
grid_col <- "grey50"
grid_size <- 0.6
major_grid <- 0:10 * 2
p <- ggplot(df3, aes(what, units)) +
fill_images() +
clip_images() +
geom_bar(fill=NA, colour="darkgreen", size=1.2, alpha=0.5, stat="identity") +
coord_flip() +
scale_y_continuous(breaks=seq(0, 20, 2)) +
scale_x_discrete() +
theme_bw() +
theme(axis.title.x = element_blank(), axis.title.y = element_blank(),
panel.grid.major.x = element_line(colour = grid_col, size = grid_size),
panel.grid.major.y = element_line(colour = NA))
p
为了保存 .svg 文件,请使用例如
ggsave(file="test.svg", plot=p, width=10, height=8)
如果您想要将填充图像作为 .svg 文件,请查看 grImport
package 。看来您必须手动将 .svg 转换为 .ps(例如使用 imagemagick),然后按照指南进行操作。
关于r - geom_bar() + 象形图,怎么做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25014492/
stackoverflow如何得知JavaScript无法正常工作,并能够在页面顶部通知用户? 是否可以判断脚本是否未加载或产生错误,然后仍然能够使用JavaScript生成错误消息? 最佳答案
Xcode 项目方案共享...如何做到这一点? 当我将 Xcode 项目提交到 SVN 时,我发现我创建的方案保存在我的用户名文件夹下,例如 abc.xcodeproj/xcuserdata/_my_
我有这个 SQL: DROP TABLE MISSINGTABLE; CREATE TABLE MISSINGTABLE ( TABLE_NAME VARCHAR2 (70), DESCRIP
我是PHP OOP的初学者,并对使用PHP处理错误的正确方法有些疑问。 例如看这个功能: public function deleteFileFromDisk($fileNameToBeDeleted
YouTube上有很多视频,我们希望为网站访问者提供自动的YouTube成绩单对齐方式。我们想要的几乎就像this example。 但是,我们希望逐个句子对齐,而不是逐个单词自动对齐,例如this
假设,我有 IAsynchronous 接口(interface),它支持两种执行某些操作的方法(“开始/结束”模式): IAsyncResult BeginOperation(AsyncCallba
Hardware Product 1 Product 2 Product 3 Product 4 Product 5 我有这样的结构,我想做一个重新排序界面,用户可以通过单击向上箭头在层次结构中向
假设,我有 IAsynchronous 接口(interface),它支持两种执行某些操作的方法(“开始/结束”模式): IAsyncResult BeginOperation(AsyncCallba
我正在使用 Silverlight 2.0 Unleashed + Silverlight 4.0 Unleashed 学习 Silverlight,好吧,我只是在玩弄它:-) 作为其中的一部分,我正
有人可以解释一下我还是链接-我有512x512图标,但我不知道我需要创建什么图标大小以及如何将它们添加到我的iOS应用中。我需要什么尺寸以及如何添加尺寸? 最佳答案 简而言之:非视网膜iPhone或i
我想在 Java 中模拟以下情况,但我陷入困境: 特别是与客户、预订、航类预订和巴士预订相关的部分。我想要一组客户对象,每个对象都有自己的预订列表(可以是航类预订或巴士预订)。 我计划像下面的测试代码
我在 opencv、Pillow、ImageMagick、subprocess 和 ffmpeg 之间摇摆,作为操作图形数据的一种方式。 ImageMagick 看起来不错而且功能相当强大,但我在 W
我想做类似的事情 SELECT t.subtitle FROM temp t LEFT JOIN ep e ON e.subtitle=t.subtitle AND e.epi
Frame[] test = new Frame[3] {{2,5},{3,6},{4,7}}; 数组初始化器只能用在变量或字段初始化器中。尝试改用新表达式。 这怎么可能? 最佳答案 这里的问题是文字
我不知道如何正确创建第一个返回。它会像这样工作,但问题是 searchtestarrayone 总是有不同的长度,而且它可能非常大。几周前开始了我的 Swift 之旅,所以下面的代码中可能有一些愚蠢的
我有这样的表: NameSteve Martin PhoneXXX Bank account654861/46147 我对表格的相同部
我有一个关于单选按钮的快速问题,以及当用户返回页面时如何设置它们。我现在想要的是能够在他们返回页面时显示所选项目。同一组中有几个,所以我不能使用 getElementByID(遗憾!)。 这是我的 H
我做了一些事情: class Tuple1 { private T1 a; private T2 b; public Tuple1(T1 a, T2 b) {
我目前正在研究我在大学的期末项目,它看起来像 instagram。在 instagram android 应用程序中,您可以点击并按住图像和 boom,显示弹出窗口。但我不知道该怎么做! 最佳答案 您
我正在使用来自 mourner/suncalc 的函数这让我可以得到我们太阳的当前位置。使用 getPosition(),我想在图像上或使用纯 CSS(当然可以缩放到不同的屏幕分辨率和方向)创建动画,
我是一名优秀的程序员,十分优秀!