美好的一天,我希望使用ggplot2生成图形,但不使用其默认的分类变量排序(按字母顺序排列,在脚本中:字母),但使用连续变量的相关值(在脚本中:数字).
这是一个示例脚本:
library(ggplot2)
trial<-data.frame(letters=letters, numbers=runif(n=26,min=1,max=26))
trial<-trial[sample(1:26,26),]
trial.plot<-qplot(x=numbers, y=letters, data=trial)
trial.plot
trial<-trial[order(trial$numbers),]
trial.plot<-qplot(x=numbers, y=letters, data=trial)
trial.plot
trial.plot+stat_sort(variable=numbers)
Run Code Online (Sandbox Code Playgroud)
最后一行不起作用.
如果我做一个图表g:
g <- read.table(text="
A B W
1 55 3
2 55 5
3 99 6 ",header=TRUE)
library(igraph)
g <- graph.data.frame(g)
Run Code Online (Sandbox Code Playgroud)
和坐标矩阵:
y<-1:5
x<-c(0.1,0.1,0.2,0.2,0.8)
l<-data.frame(x,y)
l<-as.matrix(l)
Run Code Online (Sandbox Code Playgroud)
我可以根据自定义坐标和绘图轴绘制具有节点位置的图形.
plot(g,layout=l,rescale=F,axes=TRUE,ylim=c(0,6),xlim=c(0,1))
Run Code Online (Sandbox Code Playgroud)

但是xaxis限制功能不正常,我认为是由yaxis限制改变的.我如何以他想要的方式控制x轴,例如将其保持在0和1之间.
即 plot(x,y,xlim=c(0,1),ylim=c(0,6))
这是一个错误吗?如果是,并且这无法解决是否有另一个具有相同功能的包?
我希望在函数返回错误而不是代码暂停时获得NA.
我目前正在使用
try.test<-try(results<-lm(log(0)~1))
if(class(try.test)=="try-error"){results<-NA}
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用tryCatch.
我想找到一个功能/线路解决方案.
我想将一个完整的ggplot对象旋转90°.
我不想使用,coord_flip因为这似乎干扰scale="free"和space="free"使用facet时.
例如:
qplot(as.factor(mpg), wt, data=mtcars)+
facet_grid(.~vs + am, scale="free",space="free")
Run Code Online (Sandbox Code Playgroud)

VS
qplot(as.factor(mpg), wt, data=mtcars)+
facet_grid(vs + am ~ ., scale="free",space="free")+
coord_flip()
Run Code Online (Sandbox Code Playgroud)

我想要的是什么:

我可能需要使用gridExtra.
我正在寻找一种更通用的方法从data.frame到多维数组.
我希望能够根据需要从数据框中的尽可能多的变量创建所需数量的维度.
目前,该方法必须针对每个data.frame进行定制,需要转租以形成向量.
我会喜欢plyr的熔化/铸造方法.
data<-data.frame(coord.name=rep(1:10, 2),
x=rnorm(20),
y=rnorm(20),
ID=rep(c("A","B"), each=10))
data.array<-array(dim=c(10, 2, length(unique(data$ID))))
for(i in 1:length(unique(data$ID))){
data.array[,1,i]<-data[data$ID==unique(data$ID)[i],"x"]
data.array[,2,i]<-data[data$ID==unique(data$ID)[i],"y"]
}
data.array
, , 1
[,1] [,2]
[1,] 1 1
[2,] 3 3
[3,] 5 5
[4,] 7 7
[5,] 9 9
[6,] 1 1
[7,] 3 3
[8,] 5 5
[9,] 7 7
[10,] 9 9
, , 2
[,1] [,2]
[1,] 2 2
[2,] 4 4
[3,] 6 6
[4,] 8 8
[5,] 10 10
[6,] 2 2
[7,] 4 …Run Code Online (Sandbox Code Playgroud) 在ggplot2中,即使不存在给定的级别组合,我也希望方框图中的方框宽度相等。
例如,在mtcars中,cyl = 8和gear = 4不存在,这导致该图中的条形变大:
qplot(data=mtcars, x=as.factor(cyl), y=mpg,
colour=as.factor(gear), geom="boxplot")
Run Code Online (Sandbox Code Playgroud)

对于条形图,使用这些级别组合的NA值填充数据框可以解决问题,而对于箱形图则不能解决问题:
mtcars.fill <- data.frame(cyl=8,gear=4,mpg=NA)
mtcars <- rbind.fill(mtcars,mtcars.fill)
qplot(data=mtcars, x=as.factor(cyl), y=mpg, colour=as.factor(gear), geom="boxplot")
Warning message:
Removed 1 rows containing non-finite values (stat_boxplot).
Run Code Online (Sandbox Code Playgroud)
这导致了完全相同的情节。
stat_boxplot有一个用于na值的参数,但默认设置为不删除NA:
na.rm = FALSE
Run Code Online (Sandbox Code Playgroud) 我希望将ggplot2图例旋转90°
从
qplot(mpg, wt, data=mtcars, colour=cyl)
Run Code Online (Sandbox Code Playgroud)

生产

一些geom模糊了传奇中其他geom的关键(特别是boxplot)
如何选择图例中出现的geom?
例如.:
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)+
geom_boxplot()
Run Code Online (Sandbox Code Playgroud)

切换geoms的顺序有帮助
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment,
geom="boxplot")+
geom_point()
Run Code Online (Sandbox Code Playgroud)

但我希望通过以下方式找到传说:
qplot(data=CO2,
x=Type,
y=uptake,
colour=Plant,
shape=Treatment)
Run Code Online (Sandbox Code Playgroud)

我是否需要提取一个图的图例并使用gridExtra之类的东西将其粘贴到另一个图上?
给定一个函数,如何将它保存到R脚本(.R)?
保存适用于数据,但显然无法创建.R数据.
将控制台中的粘贴复制到新的脚本文件似乎会引入导致错误的字符.
在grep从查找文件的长行中的模式文件中找到一个短模式的地方,我需要一个工具来允许我提取可以在更长模式中找到的查找文件的短行.
换句话说,鉴于莎士比亚的作品每行一句并说法语词典,我想找到哪些法语单词在莎士比亚的哪一行中找到,从而可以发现莎士比亚的一行可能包含更多的事实不止一个法语单词,而且法语单词可能出现在莎士比亚的不止一行中.
例如:
pattern_file={
"The sun is shining!"
"It is a beautiful day!"}
lookup_file={
"Rain"
"Sun"
"Cloud"
"Beautiful"
"Shining"}
Run Code Online (Sandbox Code Playgroud)
我想要的是什么
function file pattern
Run Code Online (Sandbox Code Playgroud)
给出在较长模式中找到的行和由昏迷分隔的较长模式本身,同时检测多个匹配.
ideal_result_file={
"Sun","The sun is shining!"
"Beautiful","It is a beautiful day!",
"Shining", "The sun is shining!"}
Run Code Online (Sandbox Code Playgroud)
目前,我使用grep逐行遍历整个查找文件:
while read line
do
grep -is $line pattern_file | sed 's/^/'"$line"'\,/g' >> result_file.csv
done < lookup_file
Run Code Online (Sandbox Code Playgroud)
这非常慢!我的lookup_file包含超过50 000行,而我的pattern_file包含500.使用grep在lookup_file中查找更短的模式需要几秒钟,使用我的循环方法的单次传递需要一天/几周.
任何语言的解决方案将不胜感激.
与
在大型数据集上使用grep或fgrep的非常慢的循环
相比,Perl比bash更快吗?
该解决方案需要与GB大小的循环和模式文件兼容.
我经常使用d_ply来制作探索性情节.
一个简单的例子:
require(plyr)
plot_species <- function(species_data){
p <- qplot(data=species_data,
x=Sepal.Length,
y=Sepal.Width)
print(p)
}
d_ply(.data=iris,
.variables="Species",
function(x)plot_species(x))
Run Code Online (Sandbox Code Playgroud)
它产生三个独立的地块,每个物种一个.
我想使用dplyr中的函数重现此行为.
这似乎需要在由summarize调用的函数中重新组装data.frame,这通常是不切实际的.
require(dplyr)
iris_by_species <- group_by(iris,Species)
plot_species <- function(Sepal.Length,Sepal.Width){
species_data <- data.frame(Sepal.Length,Sepal.Width)
p <- qplot(data=species_data,
x=Sepal.Length,
y=Sepal.Width)
print(p)
}
summarise(iris_by_species, plot_species(Sepal.Length,Sepal.Width))
Run Code Online (Sandbox Code Playgroud)
可以将data.frame的某些部分传递给直接汇总调用的函数,而不是传递列吗?
我经常根据数字变量进行调查,但我希望facet标签比简单数字更具解释性.我通常创建一个新的标签变量,其数字值粘贴到解释性文本.但是,当值在小数点前有多个位置时,第一个数字用于对因子进行排序.有什么建议可以避免吗?
iris[,1:4]<-iris[,1:4]*10
Run Code Online (Sandbox Code Playgroud)
当它在小数点之前没有多于一个值时,这对虹膜可以正常工作.
iris$Petal.Width.label<-paste("Petal.Width=", iris$Petal.Width)
iris$Petal.Width.label<-paste("Petal.Width=", iris$Petal.Width)
qplot(data=iris,
x=Sepal.Length,
y=Sepal.Width,
colour=Species)+facet_wrap(~Petal.Width.label)
Run Code Online (Sandbox Code Playgroud)
