这是我的树:
tree = data.frame(branchID = c(1,11,12,111,112,1121,1122), length = c(32, 21, 19, 5, 12, 6, 2))
> tree
branchID length
1 1 32
2 11 21
3 12 19
4 111 5
5 112 12
6 1121 6
7 1122 2
Run Code Online (Sandbox Code Playgroud)
这棵树是2D的,由树枝组成.每个分支都有一个ID.1是树干.然后,行李箱分叉成两个分支,11左侧和12右侧.11在分支中分叉111(向左)和112(向右)分叉.等.每个分支都有一定的长度.
在这棵树上有松鼠:
squirrels = data.frame(branchID = c(1,11,1121,11,111), PositionOnBranch = c(23, 12, 4, 2, 1), name=c("FluffyTail", "Ginger", "NutCracker", "SuperSquirrel", "ChipnDale"))
> squirrels
branchID PositionOnBranch name
1 1 23 FluffyTail …Run Code Online (Sandbox Code Playgroud) 由于几个原因,我试图复制下面显示的怪诞情节.它违反了许多良好的数据可视化规则,因此出于培训目的,我的目标是使用ggplot2和解构它 - 一次删除或修改选择不当的功能.使用底部复制的数据和图表下方的代码,我越来越接近,但一直无法弄清楚如何包含一个值得注意的功能.
问题:有没有办法在三个刻度标签周围重现黑色阴影矩形?(如果是这样,可以直接创建另一个因子变量来识别这三个标签并将其字体更改为白色.)

ggplot(plotpg19, aes(x = risks, y = scores, fill = colors)) +
geom_bar(stat = "identity", width = 0.6) +
scale_fill_manual(values = c("grey50", "deepskyblue2", "mediumorchid3", "gold")) +
geom_text(aes(label = scores), hjust = -0.4, size = 8, face = "bold") +
coord_flip() +
theme_bw() + labs(x = NULL, y = NULL) +
theme(panel.grid.major = element_blank()) +
guides(fill = FALSE) +
scale_y_continuous(breaks = seq(0, 100, 20), labels = seq(0, 100, 20), expand = c(0, 0)) +
theme( …Run Code Online (Sandbox Code Playgroud) 我的目标是重现这个情节,我的问题是在每个条形图中重现渐变填充.
COMMENT后添加 @PavoDive的好评指导我们的一个问题,基本上说:"你不能用GGPLOT2做到这一点,此外这是一个坏主意,即使你能做到这一点." 同意它是一个糟糕的图形选择,但出于教学目的,我想重新创建原始,然后显示改进.那么,是否有编程解决方案呢?
随着ggplot代码之后的数据我已经接近了,除了每个条形码(和微小的刻度线)相同的一致渐变着色.但是我的努力导致填充的条形与y值匹配,而在原始图形中,每个条形都填充相同的模式.我如何实现这种效果?
ggplot(df, aes(x = x, y = y, fill = y)) +
geom_hline(yintercept = seq(0, .35, .05), color = "grey30", size = 0.5, linetype = "solid") +
geom_bar(stat = "identity", width = 0.4) +
scale_fill_gradient(low='green4', high='green1', guide = FALSE) +
theme(legend.position = "none") +
theme_minimal() +
geom_text(data = df, aes(label = scales::percent(y), vjust = -.5)) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks = element_blank()) +
labs(y = "", x = "") +
ggtitle("Question …Run Code Online (Sandbox Code Playgroud) 是否可以使用引入的笔触参数ggplot2 2.0来调整条形周围的边框厚度?如果没有,有没有办法控制沿边界线厚度线的条形边框厚度? 笔划适用于某些形状的边框 - 请参阅第二个答案
一个非常适度的MWE,仅显示填充:
factor <- c("One", "Two", "Three", "Four")
value <- c(1, 2, 3, 4)
factor2 <- c("A", "B", "A", "B")
df <- data.frame(factor = factor(factor, levels = factor),
value = value, factor2 = factor2)
ggplot(df, aes(x = factor, y = value, color = factor2)) +
geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)
在COMMENT
OK 之后编辑,感谢MLavoie的评论,它非常简单.这是我已经结束的代码,并且,不,我实际上并没有使用这个情节,只是教ggplot它和它的能力.
ggplot(df, aes(x = factor, y = value, color = factor2)) +
scale_color_manual(values = c("darkgreen", "slateblue4")) +
geom_bar(stat = "identity", aes(fill …Run Code Online (Sandbox Code Playgroud) 如果这是一个简单的问题,请原谅我的无知,但我似乎无法弄清楚如何强调情节标题的任何部分.我正在使用ggplot2.
我能找到的最好的是 手工制作的注释("段"),我创建了一个玩具图来说明它的方法.
df <- data.frame(x = 1:10, y = 1:10)
rngx <- 0.5 * range(df$x)[2] # store mid-point of plot based on x-axis value
rngy <- 0.5 * range(df$y)[2] # stores mid-point of y-axis for use in ggplot
ggplot(df, aes(x = x, y = y)) +
geom_point() +
ggtitle("Oh how I wish for ..." ) +
ggplot2::annotate("text", x = rngx, y = max(df$y) + 1, label = "underlining!", color = "red") +
# create …Run Code Online (Sandbox Code Playgroud) 有没有办法保持在地图边界内的地图上抖动的点?在下面的示例中,康涅狄格州西南部的抖动位置最终出现在水中或相邻的状态中,是否有办法让R抖动位置点而不是地图边界?
或者,是否有其他一些技术,例如在每个城市附近创建一个表格,以列出公司的名称?
# create a data frame called "ct" of geolocations in two cities near the border of a US state (Connecticut). Each firm has the same lat and longitude of one of the two cities
> dput(ct)
structure(list(city = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), .Label = c("Greenwich", "Stamford"), class = "factor"),
firm = structure(c(1L, 12L, 21L, 22L, …Run Code Online (Sandbox Code Playgroud) 下面的迷人图表是经济学人,2016年1月30日,第61页.它描绘了从五个地区到六个地区的液化天然气(LNG)出口.R如何绘制类似于它的东西,也许有几个Sankey图(从包中riverplots)但箭头反转,如图所示的箭头?这意味着,流入出口地区的流量将显示为流出.
通过眼球从图中提取数据导致df数据帧.变量有奇怪的名称,因为`riverplot需要唯一的节点名称.
> dput(df)
structure(list(ID = structure(c(1L, 6L, 9L, 13L, 2L, 7L, 14L,
3L, 10L, 15L, 4L, 11L, 5L, 8L, 12L, 16L), .Label = c("Africa-Asia",
"Africa-Europe", "Africa-Nam", "Africa-SE", "Africa-SthAm", "Europe-Asia",
"Europe-Europe", "Europe-SthAm", "MidEast-Asia", "MidEast-NthAm",
"MidEast-SE", "MidEast-SthAm", "SE Asia-Asia", "Sth Am.-Eur",
"Sth Am.-NthAm", "Sth Am.-SthAm"), class = "factor"), x = c(-30,
1, 20, 100, -30, 1, -100, -30, 20, -100, -30, 20, -30, 1, 20,
-100), y = c(120, 120, 120, 120, 1, 1, 1, -120, -120, …Run Code Online (Sandbox Code Playgroud) 从URL的字符向量开始.我们的目标是与公司的唯一名称最终,只意味着一列"test","example"并"sample"在下面的例子.
urls <- c("http://grand.test.com/", "https://example.com/",
"http://.big.time.sample.com/")
Run Code Online (Sandbox Code Playgroud)
删除".com"可能跟随它的任何内容并保留第一部分:
urls <- sapply(strsplit(urls, split="(?<=.)(?=\\.com)", perl=T), "[", 1)
urls
# [1] "http://grand.test" "https://example" "http://.big.time.sample"
Run Code Online (Sandbox Code Playgroud)
我的下一步是使用链式调用删除http://和https://部分gsub():
urls <- gsub("^http://", "", gsub("^https://", "", urls))
urls
# [1] "grand.test" "example" ".big.time.sample"
Run Code Online (Sandbox Code Playgroud)
但这是我需要帮助的地方.如何在第一个和第三个网址字符串中处理公司名称之前的多个句点(点)?例如,下面的调用返回第二个字符串的NA,因为该"example"字符串没有剩余句点.或者,如果我只保留第一部分,我会丢失公司名称.
urls <- sapply(strsplit(urls, split = "\\."), "[", 2)
urls
# [1] "test" NA "big"
urls <- sapply(strsplit(urls, split = "\\."), "[", 1)
urls
# [1] "grand" "example" ""
Run Code Online (Sandbox Code Playgroud)
也许是一个 …
我有一个婴儿名字的文件,我正在阅读,然后试图得到婴儿名字中的最后一个字符.例如,该文件看起来像..
Name Sex
Anna F
Michael M
David M
Sarah F
Run Code Online (Sandbox Code Playgroud)
我在使用中读到了这个
sourcenames = read.csv("babynames.txt", header=F, sep=",")
Run Code Online (Sandbox Code Playgroud)
我最终希望结果看起来像......
Name Last Initial Sex
Michael l M
Sarah h F
Run Code Online (Sandbox Code Playgroud)
我已设法将名称拆分为单独的字符..
sourceout = strsplit(as.character(sourcenames$Name),'')
Run Code Online (Sandbox Code Playgroud)
但是现在我被困在哪里是如何得到最后一封信,所以在迈克尔的情况下,如何得到'我'.我认为tail()可能会工作,但它会返回最后几条记录,而不是每个Name元素中的最后一个字符.
非常感谢任何帮助或建议.
谢谢 :)
考虑
text <- "who let the dogs out"
fooo <- strsplit(text, " ")
fooo
[[1]]
[1] "who" "let" "the" "dogs" "out"
Run Code Online (Sandbox Code Playgroud)
输出strsplit是一个列表.然后列表的第一个元素是一个向量,其中包含上面的单词.
为什么函数表现那样?是否有任何情况会返回包含多个元素的列表?
我可以使用
fooo[[1]][1]
[1] "who"
Run Code Online (Sandbox Code Playgroud)
,但有没有更简单的方法?
r ×10
ggplot2 ×7
strsplit ×3
parsing ×2
plot ×2
regex ×2
axis-labels ×1
choropleth ×1
choroplethr ×1
geom-bar ×1
geospatial ×1
gradient ×1
graph ×1
grob ×1
plotmath ×1
string ×1
substring ×1
tree ×1
underline ×1