我想在ggplot中添加一个突出显示的站点坐标表.
使用上一个问题作为示例数据:
set.seed(1)
mydata <- data.frame(a=1:50, b=rnorm(50))
ggplot(mydata,aes(x=a,y=b)) +
geom_point(colour="blue") +
geom_point(data=mydata[10:13, ], aes(x=a, y=b), colour="red", size=5)
Run Code Online (Sandbox Code Playgroud)
我想将下表添加到绘图区域内绘图的右下角.有什么建议?
table<-cbind(sites=c("site 1","site 2","site 3","site 4"),mydata[10:13,])
table
sites a b
site 1 10 -0.3053884
site 2 11 1.5117812
site 3 12 0.3898432
site 4 13 -0.6212406
Run Code Online (Sandbox Code Playgroud) 我需要删除一组向量中的最后一个数字,即:
v <- 1:3
v1 <- 4:8
Run Code Online (Sandbox Code Playgroud)
应成为:
v <- 1:2
v1 <- 4:7
Run Code Online (Sandbox Code Playgroud) 如何增加ggplot 2中绘图区域周围的区域,使我的轴标题有一些喘息空间.我知道vjust和hjust(如下所示),然而,我似乎无法在绘图区域周围创建实际空间来移动我的轴标题.
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p
p<- p + theme(axis.title.x = element_text(family="Times",size=20,face="bold",colour = "Black",vjust=-1,hjust=0.5))
p
Run Code Online (Sandbox Code Playgroud)
如何将y轴标签从绘图区域的左侧移动到右侧,将x轴标签从下方移动到下图中的绘图区域上方?谢谢
xleft<-c(1,2,2.5)
xright<-c(2,2.5,2.75)
ybottom<-c(1,2,2.5)
ytop<-c(2,2.5,2.75)
par(mar = c(15,15,2.75,2.75) + 0.1)
plot(c(1,3),c(1,3),type="n",main="title",xlab="xlab-move me above plot",ylab="ylab-move me right of plot",axes=F,asp=1)
axis(1,pos=1)
axis(2,pos=1)
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))
#Label position along axes
x.label.position<-(xleft+xright)/2
y.label.position<-(ybottom+ytop)/2
#Labels
x.label<-c("Long species Name1","Long species Name2","Long species Name3")
y.label<-c("Long species Name4","Long species Name5","Long species Name5")
text(par()$usr[1]-0.5,y.label.position,y.label,xpd=TRUE,adj=1)
text(y=par()$usr[3]-0.5,x=x.label.position,x.label,xpd=TRUE,adj=1,srt=90)
par(xpd=TRUE)
legend(-0.1,0,legend=c("Species A","Species B","Species C"),fill=c("blue", "red", "green"))
Run Code Online (Sandbox Code Playgroud)
反正有没有让grid.arrange()充当split.screen()?我想安排一张桌子直接位于图例下方.
#create histogram
my_hist<-ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
#create inset table
my_table<- tableGrob(head(diamonds)[,1:3],gpar.coretext =gpar(fontsize=8),gpar.coltext=gpar(fontsize=8), gpar.rowtext=gpar(fontsize=8))
grid.arrange(my_hist,my_table, ncol=2)
Run Code Online (Sandbox Code Playgroud)
生产:
但我希望它看起来像这样:
我尝试了split.screen()但它似乎不适用于ggplot类型的图形.有什么建议?谢谢.
我绘制了以下数据并添加了黄土更平滑.我想在图中添加一个三阶多项式及其方程(包括残差).有什么建议?
set.seed(1410)
dsmall<-diamonds[sample(nrow(diamonds), 100), ]
df<-data.frame("x"=dsmall$carat, "y"=dsmall$price)
p <-ggplot(df, aes(x, y))
p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)
#Add a loess smoother
p<- p + geom_smooth(method="loess",se=TRUE)
Run Code Online (Sandbox Code Playgroud)
如何添加三阶多项式?我试过了:
p<- p + geom_smooth(method="lm", se=TRUE, fill=NA,formula=lm(y ~ poly(x, 3, raw=TRUE)),colour="red")
Run Code Online (Sandbox Code Playgroud)
最后,如何将三阶多项式方程和残差加到图中?我试过了:
lm_eqn = function(df){
m=lm(y ~ poly(x, 3, df))#3rd degree polynomial
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq))
}
data.label …
Run Code Online (Sandbox Code Playgroud) 当我创建下面的绘图时,我会在绘图区域和轴之间产生不需要的空间(即蓝色框和x轴之间的空白区域.如何删除此空间并使绘图区域与绘图轴齐平?谢谢.
xleft<-c(1,2,2.5)
xright<-c(2,2.5,2.75)
ybottom<-c(1,2,2.5)
ytop<-c(2,2.5,2.75)
par(mar = c(15,15,2.75,2.75) + 0.1)
plot(c(1,2.75),c(1,2.75),type="n",main="title",xlab="site.x",ylab="ylab")
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))
#Label position along axes
x.label.position<-(xleft+xright)/2
y.label.position<-(ybottom+ytop)/2
#Labels
x.label<-c("Long species Name1","Long species Name2","Long species Name3")
y.label<-c("Long species Name4","Long species Name5","Long species Name5")
text(par()$usr[1]-0.5,y.label.position,y.label,xpd=TRUE,adj=1)
text(y=par()$usr[3]-0.5,x=x.label.position,x.label,xpd=TRUE,adj=1,srt=90)
par(xpd=TRUE)
legend(-0.1,0,legend=c("Species A","Species B","Species C"),fill=c("blue", "red", "green"))
Run Code Online (Sandbox Code Playgroud)
更新我尝试了plannapus的建议和我的实际数据,但是只能让y轴行为是否在这块代码中还有其他一些东西给绘图区域的边增加了空间?
quartz("colour.plot")
par(mar=c(15,15,4,2)+0.1)#sets margins of plotting area
#create the data plot
plot(c(0,100), c(0,100), type = "n", main = paste(x,"vs",y," -",depth),xlab=paste("Species composition in remainder ",x),ylab=paste("Species composition in remainder ",y),asp=1,xaxs="i",yaxs="i")
#Add the rectangles
rect(mdf$xleft,mdf$ybottom,mdf$xright,mdf$ytop,col=mdf$colour)
Run Code Online (Sandbox Code Playgroud)
产生
我有一个矩阵
df<-matrix(data=c(3,7,5,0,1,0,0,0,0,8,0,9), ncol=2)
rownames(df)<-c("a","b","c","d","e","f")
[,1] [,2]
a 3 0
b 7 0
c 5 0
d 0 8
e 1 0
f 0 9
Run Code Online (Sandbox Code Playgroud)
我想先按列1降序排列矩阵,然后按列2排序,得到矩阵
df.ordered<-matrix(data=c(7,5,3,1,0,0,0,0,0,0,9,8),ncol=2)
rownames(df.ordered)<-c("b","c","a","e","f","d")
[,1] [,2]
b 7 0
c 5 0
a 3 0
e 1 0
f 0 9
d 0 8
Run Code Online (Sandbox Code Playgroud)
有关如何实现这一目标的任何建议?谢谢.
可能重复:
替换R数据帧中因子列的内容
我有data.frame
df1<-data.frame("Sp1"=1:6,"Sp2"=7:12,"Sp3"=13:18)
rownames(df1)=c("A","B","C","D","E","F")
df1
Sp1 Sp2 Sp3
A 1 7 13
B 2 8 14
C 3 9 15
D 4 10 16
E 5 11 17
F 6 12 18
Run Code Online (Sandbox Code Playgroud)
我想用df1 $ Sp2列中的数字8的每个条目替换为数字800.我试过:
test<-replace(df1$Sp2,df1[800,"Sp2"],5)
Run Code Online (Sandbox Code Playgroud) 有关如何提高geom_text分辨率以使分辨率与轴标签的分辨率相当的任何建议?谢谢
df <- data.frame("x" = c(1,2,3,4),"y" = c(15,19,35,47))
p<-ggplot(df,aes(x,y))
p<- p + geom_point(size=1)
p<- p + geom_smooth(method="lm", se=FALSE, formula=y~x)
p<- p + xlab("Better Resolution")
p<- p +ylab("Better Resolution")
p<- p +opts(axis.title.x = theme_text(family="Times",face="bold", size=25, colour = "Black",vjust=0))
p<- p +opts(axis.title.y = theme_text(family="Times",face="bold", size=25, angle =90, colour ="Black",vjust=0.4))
p<- p + geom_text(aes(x = 3.5, y = 37, label ="123456789"),size=12, parse = TRUE)
p
#The zoomed in text looks like this after saving using ggsave
Run Code Online (Sandbox Code Playgroud)
#Information about my version of R and …
Run Code Online (Sandbox Code Playgroud)