我想将图例标题sex稍微移动到图例框的水平中心.我试过theme和guide_legend,但失败了.两种方式都不会改变图例标题的位置.
# example data from http://www.cookbook-r.com/Graphs/Legends_(ggplot2)/
df1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42)
)
library(ggplot2)
p <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) +
geom_line() + geom_point()
# no change
p + theme(legend.title = element_text(hjust = 0.5))
p + guides(color=guide_legend(title.hjust=0.5))
Run Code Online (Sandbox Code Playgroud)
另外,我使用的是ggplot2_2.2.0.
我有一个图形组装patchwork如下:
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p1 + p2 + plot_annotation(tag_levels = 'a')
Run Code Online (Sandbox Code Playgroud)
如何更改标签的字体以使字母a和b以粗体显示?
我试图删除列中data.frame的值posn不在另一个中给出的范围内的行data.frame,具有data.table非equi连接功能.
以下是我的数据的样子:
library(data.table)
df.cov <-
structure(list(posn = c(1, 2, 3, 165, 1000), att = c("a", "b",
"c", "d", "e")), .Names = c("posn", "att"), row.names = c(NA,
-5L), class = "data.frame")
df.exons <-
structure(list(start = c(2889, 2161, 277, 164, 1), end = c(3329,
2826, 662, 662, 168)), .Names = c("start", "end"), row.names = c(NA,
-5L), class = "data.frame")
setDT(df.cov)
setDT(df.exons)
df.cov
# posn att
# 1: 1 a
# 2: 2 b
# …Run Code Online (Sandbox Code Playgroud) 我使用facet_wrap绘制了由两个因子分组的2d面板.
最小的例子:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ cyl + drv)
Run Code Online (Sandbox Code Playgroud)
每个面板顶部的标签有两行,我想减少两行标签文本之间的间距.我怎么这么正确呢?
我试过了:
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
facet_wrap(~ cyl + drv) +
theme(
strip.text = element_text(
margin = margin(t = 0, r = 0, b = 0, l = 0, unit = "pt")
)
)
Run Code Online (Sandbox Code Playgroud)
但这没用.
提前致谢.
虽然这很容易用碱R或做setnames in data.table或rename_在dplyr0.5.由于rename_不推荐使用,我无法在dplyr0.6.0中找到一种简单的方法.
以下是一个例子.我想col.from用以下相应的值替换列名col.to:
col.from <- c("wt", "hp", "vs")
col.to <- c("foo", "bar", "baz")
df <- mtcars
head(df, 2)
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Mazda RX4 21 6 160 110 3.9 2.620 16.46 0 1 4 4
#> Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
Run Code Online (Sandbox Code Playgroud)
预期产量:
names(df)[match(col.from, names(df))] <- col.to
head(df, …Run Code Online (Sandbox Code Playgroud) 这是我目前拥有的示例:
x <- head(mtcars)
x$rn <- rownames(x)
x$rn[5] <- 'Hornet\nSportabout'
library(ggplot2)
ggplot(x, aes(x = rn, y = mpg)) + geom_point() +
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))
ggsave('test.png', width = 3, height = 3)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,'Hornet\nSportabout'离下一个太近了,我想把它向左移动一点。预期的 x 轴文本如下所示:
我认为我应该设置vjust = 0.5,但这也会将轴文本向下移动。我也试过的severial组合vjust和hjust仍然无法得到所需的输出。有没有办法axis.text像第二个图(用 libreoffice draw 手动创建)所示与 ggplot2 对齐?
这篇文章是基于这个问题:( 迭代列表以获得其名称的价值)
我会在这里重现它:
test_list <- list(list("name"="A","property"=1),
list("name"="B","property"=2),
list("name"="C","property"=3))
Run Code Online (Sandbox Code Playgroud)
最初的OP发现:
sapply(test_list, `$`, "property")
Run Code Online (Sandbox Code Playgroud)
只返回NULL值.如果使用[[jogo评论时,它会给出正确的结果:
> sapply(test_list, `$`, "property")
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
> sapply(test_list, `[[`, "property")
[1] 1 2 3
Run Code Online (Sandbox Code Playgroud)
这也行不通:
> sapply(test_list, function(x, y) `$`(x, y), y = "property")
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
Run Code Online (Sandbox Code Playgroud)
然而,无论是$和[[工作的一个单独的元素test_list:
> `$`(test_list[[1]], 'property')
[1] 1
> `[[`(test_list[[1]], 'property')
[1] 1
Run Code Online (Sandbox Code Playgroud)
当我用作lapply/sapply的参数时,我想知道为什么$和[[表现不同FUN=.
使用fread. 看起来像内存问题。我尝试使用nrows=1000. 但没有运气。使用 linux
file ok but could not memory map it. This is a 64bit process. There is probably not enough contiguous virtual memory available.
Run Code Online (Sandbox Code Playgroud)
下面的代码可以read.csv用下面使用的所有选项替换吗?它有帮助吗?
rdata<- fread(
file=csvfile, sep= "|", header=FALSE, col.names= colsinfile,
select= colstoselect, key = "keycolname", na.strings= c("", "NA")
, nrows= 500
)
Run Code Online (Sandbox Code Playgroud) 我试图删除 data.frame 的 rownames 和 colnames。正如杰森所说,这可以通过将 rownamesunname设置为NULL. 我注意到手册说使用unnamewith 选项force = T可以一步完成同样的事情:
强制逻辑;如果为 true,则即使从 data.frames 中也删除了 dimnames(名称和行名称)。
但是,这对我不起作用:
d <- data.frame(a = c(x1 = 1, x2 = 2), b = c(x1 = 'a', x2 = 'b'))
unname(d, force = T)
# Error in `dimnames<-.data.frame`(`*tmp*`, value = NULL) :
# invalid 'dimnames' given for data frame
Run Code Online (Sandbox Code Playgroud)
是什么原因?
sessionInfo()
# R version 3.3.1 (2016-06-21)
# Platform: x86_64-pc-linux-gnu (64-bit)
# Running under: Ubuntu 16.04.1 LTS
# …Run Code Online (Sandbox Code Playgroud) 我知道为了计算一个子字符串的出现次数,我可以使用str.count()。然而这个功能不符合我的需求。更具体地说,假设我有字符串“MSAGARRRPR”,我想计算子字符串“RR”出现的次数。
stringr::str_count(string = "MSAGARRRPR", pattern = "RR")
将返回数字 1。但是,在当前示例中,我感兴趣的是计算“R”后面跟着另一个“R”的次数,并且这种情况发生了两次。
我写了一个函数来计算它:
occurrences <- function(string, pattern){
n <- nchar(patter)
number_pieces <- (nchar(string) - (n - 1))
pieces <- character(number_pieces)
for (i in 1:number_pieces){
pieces[i] <- substring(string, first = i, last = i + (n - 1))
}
output <- sum(pieces == pattern)
return(output)
}
Run Code Online (Sandbox Code Playgroud)
现在,ocurrences(string = "MSAGARRRPR", pattern = "RR")返回预期答案:2
尽管如此,我想知道是否有更有效的 R 函数来计算它。
提前致谢!