我知道问题在这里被问到:有没有办法增加strip.text条的高度?
我想降低strip.text栏的高度而不改变文本大小.在当前情况下,文本和条形条墙之间总是留有空间.
这是我到目前为止所尝试的,
library(gcookbook) # For the data set
library(ggplot2)
ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_grid(.~ Date) +
theme(strip.text = element_text(face="bold", size=9,lineheight=5.0),
strip.background = element_rect(fill="lightblue", colour="black",
size=1))
Run Code Online (Sandbox Code Playgroud)
在我的情况下,似乎lineheight即使更改为也不会影响任何内容5.为什么?
如何使条形尺寸稍小但保持文字大小相同?
如果只有一行,我们可以减小条带尺寸facets.
g = ggplotGrob(p)
g$heights[c(3)] = unit(.4, "cm") # Set the height
grid.newpage()
grid.draw(g)
Run Code Online (Sandbox Code Playgroud)
然而,在我的真实数据中,我有很多行如下图,当我改变g $高度的元素时,没有发生任何事情!
p = ggplot(cabbage_exp, aes(x=Cultivar, y=Weight)) + geom_bar(stat="identity") +
facet_wrap(~ Date,ncol = 1) +
theme(strip.text = element_text(face="bold", size=9),
strip.background = element_rect(fill="lightblue", colour="black",size=1))
Run Code Online (Sandbox Code Playgroud)
g = ggplotGrob(p)
g$heights …Run Code Online (Sandbox Code Playgroud) 嗨我的数据中有以下字符串,并希望将A1-A9替换为A01-A09,将B1-B9替换为B01-B09,但保留数字>=10.
rep_data=data.frame(Str= c("A1B10", "A2B3", "A11B1", "A5B10"))
Str
1 A1B10
2 A2B3
3 A11B1
4 A5B10
Run Code Online (Sandbox Code Playgroud)
这里有一个类似的帖子,但我的问题有点不同!并且在这里str_replace没有看到类似的例子.
如果您知道解决方案,将会非常高兴.
预期产出
Str
1 A01B10
2 A02B03
3 A11B01
4 A05B10
Run Code Online (Sandbox Code Playgroud) 我需要从数据中检测连续减少数量的长度为5的第一个序列的第一个元素.这里有一个类似的帖子,但当我申请我的数据时,它失败了.
set.seed(201)
az <- c(sort(runif(10,0,0.9),decreasing = T),sort(runif(3,-0.3,0),decreasing = T),sort(runif(3,-0.3,0),decreasing = F),sort(runif(4,-0.3,0),decreasing = T),sort(runif(4,-0.3,0),decreasing = F),sort(runif(6,-0.3,0),decreasing = T))
tz <- seq(1,length(az))
df <- data.frame(tz,az=round(az,2))
Run Code Online (Sandbox Code Playgroud)
在上图中,它将在tz = 25附近.
帖子说这个功能需要改进,到目前为止我无法得到我想要的结果!
getFirstBefore<-function(x,len){
r<-rle(sign(diff(x)))
n<-which(r$lengths>=len & r$values<0)
if(length(n)==0)
return(-1)
1+sum(r$lengths[seq_len(n[1]-1)])
}
df1 <- df%>%
mutate(cns_tz=getFirstBefore(az,5))
tz az cns_tz
#1 1 0.56 4
#2 2 0.55 4
#3 3 0.33 4
#4 4 0.33 4
#5 5 0.26 4
#6 6 0.15 4
#7 7 0.12 4
#8 8 0.09 4
#9 9 0.04 …Run Code Online (Sandbox Code Playgroud) 我尝试使用数据的第一行重命名列名。
将 %>% 与 colnames()<- 等替换函数一起使用
我反驳的问题是在不破坏列的情况下执行此过程,dplyr pipeline因为我想在重命名列后继续做一些其他的事情。
这篇文章中有关于rename函数 dplyr::rename 的评论,如果您只是(重新)命名许多列中的一些(它需要同时编写旧名称和新名称;请参阅@Richard Scriven 的答案)
但是,在我的真实数据中,列数不是固定的,因此我需要使用类似的方法来选择列 select(X9:max(ncol(.)))
df <- data.frame(replicate(10,sample(100,2,rep=TRUE)))
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
1 77 40 45 98 75 17 5 33 53 94
2 43 67 82 42 63 90 14 65 4 98
library(dplyr)
df1 <- df %>%
select(X8,X9,X10)%>%
....
Run Code Online (Sandbox Code Playgroud)
选择和重命名列后的预期输出
33 53 94
1 65 4 98
Run Code Online (Sandbox Code Playgroud) 我的dplyr sample_n功能有问题.我试图从data.frame中随机提取子集但它失败了.因为sample_n只提取随机行.
这里有一些示例,展示了如何从每个子集中提取随机行.
这不是我想要的.我想从数据框中随机提取组,而不是每个子集中的随机行.
例如,
xx <- rep(rep(seq(0,800,200),each=10),times=2)
yy<-c(replicate(2,sort(10^runif(10,-1,0),decreasing=TRUE)),replicate(2,sort(10^runif(10,-1,0),decreasing=TRUE)), replicate(2,sort(10^runif(10,-2,0),decreasing=TRUE)),replicate(2,sort(10^runif(10,-3,0),decreasing=TRUE)), replicate(2,sort(10^runif(10,-4,0), decreasing=TRUE)))
V <- rep(seq(100,2500,length.out=10),times=2)
No <- rep(1:10,each=10)
df <- data.frame(V,xx,yy,No)
library(dplyr)
random <- df %>%
group_by(No)%>%
sample_n(5,replace=T) ## This part is the problem.
Run Code Online (Sandbox Code Playgroud)
例如,如何随机提取3个子集并保留所有行?
V xx yy No
1 100.0000 0 0.9877468589 1
2 366.6667 0 0.6658268649 1
3 633.3333 0 0.4408336374 1
4 900.0000 0 0.4136939054 1
5 1166.6667 0 0.4104986026 1
6 1433.3333 0 0.3899468530 1
7 1700.0000 0 …Run Code Online (Sandbox Code Playgroud) 我在使用重复的实际数据行时遇到了麻烦dplyr.这里已经有另一篇文章重复数据帧的行,但没有解决方案dplyr.
在这里,我只是想知道怎么可能是解决方案,dplyr
但失败了,错误:
错误:结果大小错误(16),预期为4或1
library(dplyr)
df <- data.frame(column = letters[1:4])
df_rep <- df%>%
mutate(column=rep(column,each=4))
Run Code Online (Sandbox Code Playgroud)
预期产出
>df_rep
column
#a
#a
#a
#a
#b
#b
#b
#b
#*
#*
#*
Run Code Online (Sandbox Code Playgroud) 我在 ggplot2 中包装长文本时遇到问题。类似的问题在这里问GGPLOT2有没有一种简单的方法来包装注释文本?
我的问题是我们是否有这样的文字
my_label <- "Some_arbitrarily_larger_text"
Run Code Online (Sandbox Code Playgroud)
我们如何使用相同的方法缩小它?
wrapper <- function(x, ...) paste(strwrap(x, ...), collapse = "\n")
library(ggplot2)
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()+
annotate("text", x = 4, y = 25, label = wrapper(my_label, width = 5))
Run Code Online (Sandbox Code Playgroud)
它似乎不适用于这种情况!
我有一个数据(大数据125000行,~20 MB),其中一些具有特定字符串的行需要删除,并且在读取过程中需要选择一些列.
首先,我发现grepl函数不能正常工作,因为fread在这个问题中也将数据作为一列表示.
可以在此处找到示例数据(通过遵循@akrun建议)和这样的数据标题
头(sum_data)
TRIAL : 1 3331 9091
TRIAL : 2 1384786531 278055555
2 0.10 0.000E+00 -0.0047 -0.0168 -0.9938 -0.0087 -0.0105 -0.9709 0.0035 0.0079 -0.9754 0.0081 0.0023 0.9997 -0.135324E-09 0.278754E-01
2 0.20 0.000E+00 -0.0121 0.0002 -0.9898 -0.0364 -0.0027 -0.9925 -0.0242 -0.0050 -0.9929 0.0029 -0.0023 0.9998 -0.133521E-09 0.425567E-01
2 0.30 0.000E+00 0.0193 -0.0068 -0.9884 0.0040 0.0139 -0.9782 -0.0158 0.0150 -0.9814 0.0054 -0.0008 0.9997 -0.134103E-09 0.255356E-01
2 0.40 0.000E+00 -0.0157 0.0183 -0.9879 …Run Code Online (Sandbox Code Playgroud) 我在使用 dplyr 中的函数时遇到问题mutate,错误提示:
Error: incompatible size (0), expecting 5 (the group size) or 1
Run Code Online (Sandbox Code Playgroud)
之前有一些帖子,我尝试了一些解决方案,但我的案例没有运气。
r-dplyr-使用-mutate-with-na-omit-导致错误-不兼容-大小-d
这是我尝试过的,
ff <- c(seq(0,0.2,0.1),seq(0,-0.2,-0.1))
flip <- c(c(0,0,1,1,1,1),c(1,1,0,0,0,0))
df <- data.frame(ff,flip,group=gl(2,6))
> df
ff flip group
1 0.0 0 1
2 0.1 0 1
3 0.2 1 1
4 0.0 1 1
5 -0.1 1 1
6 -0.2 1 1
7 0.0 1 2
8 0.1 1 2
9 0.2 0 2
10 0.0 0 …Run Code Online (Sandbox Code Playgroud) 目前,我尝试在分组数据中找到集群的中心。通过使用示例数据集和问题定义,我能够为kmeans每个组创建集群。但是,当谈到为给定组解决集群的每个中心时,我不知道如何获得它们。https://rdrr.io/cran/broom/man/kmeans_tidiers.html
样本数据取 从(与用于附加小修改gr列)的示例数据
library(dplyr)
library(broom)
library(ggplot2)
set.seed(2015)
sizes_1 <- c(20, 100, 500)
sizes_2 <- c(10, 50, 100)
centers_1 <- data_frame(x = c(1, 4, 6),
y = c(5, 0, 6),
n = sizes_1,
cluster = factor(1:3))
centers_2 <- data_frame(x = c(1, 4, 6),
y = c(5, 0, 6),
n = sizes_2,
cluster = factor(1:3))
points1 <- centers_1 %>%
group_by(cluster) %>%
do(data_frame(x = rnorm(.$n, .$x),
y = rnorm(.$n, .$y),
gr="1"))
points2 <- centers_2 %>%
group_by(cluster) %>% …Run Code Online (Sandbox Code Playgroud) r ×10
dplyr ×6
ggplot2 ×2
data.table ×1
dataframe ×1
diff ×1
facet-wrap ×1
fread ×1
k-means ×1
regex ×1
str-replace ×1