我想在下图中设置x和y轴以具有相同的比例距离(即x轴上的0.1与y轴上的0.1相同).有什么建议?谢谢.
df <-data.frame(x = c(0,0.2,0.5), y = c(0.6,0.7,0.9))
p <-ggplot(df, aes(x, y, ymin=0, ymax=1, xmin=0, xmax=1))
p <- p + geom_point(alpha=2/10, shape=21, fill="blue", colour="black", size=5)
grid.arrange(p, p,ncol=1)
p
Run Code Online (Sandbox Code Playgroud)

我有矩阵
m <- matrix(c(1, 0, 3, 4, 0, 6), 3)
Run Code Online (Sandbox Code Playgroud)
我需要过滤出两个列的值都为0的行,这些行实际上返回了矩阵:
m <- matrix(c(1, 3, 4, 6), 3)
Run Code Online (Sandbox Code Playgroud)
我试过了
m[m[, 1] > 0 & m[, 2] > 0]
Run Code Online (Sandbox Code Playgroud)
但是这会返回一个向量而不是一个只有0的行的矩阵.这应该很简单,但我被卡住了.
谢谢, - 伊丽莎白
可能重复:
如何按R中的列对数据帧进行排序
我试图按几列对data.frame进行排序
df<-data.frame("Sp1"=c(7,4,2),"Sp2"=c(6,2,1))
row.names(df)<-c("A01","A02","A03")
Sp1 Sp2
A01 7 6
A02 4 2
A03 2 1
#I am using
df[with(df, order("Sp1"))]
Run Code Online (Sandbox Code Playgroud)
但这没有任何作用.有什么想法吗?谢谢
如何将x轴上的标签对中以匹配条形图?另外,如何将x轴标签进一步向下定位,使其不被x轴标签遮挡?谢谢!
par(mar= c(15,4,4,2) + 0.1)
barplot(58:1,xaxt="n",xlab="",ylab="Frequency", col=gray(5:0/5))
axis(1, labels=FALSE)
text(1:58, par("usr")[3] - 0.25, srt = 90, adj = 1,
labels = rep("Long Species Name",58), xpd = TRUE)
mtext(1, text = "Species", line=6)
Run Code Online (Sandbox Code Playgroud)

我有以下情节
xleft<-c(1,2,3)
xright<-c(2,3,4)
ybottom<-c(1,2,3)
ytop<-c(2,3,4)
plot(c(1,4),c(1,4),type="n",main="title",xlab="site.x",ylab="ylab")
rect(xleft,ybottom,xright,ytop,col=c("blue","red","green"))
Run Code Online (Sandbox Code Playgroud)
我想使用这些位置和标签向两个轴添加相当长的物种名称
#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")
x.label<-c("Long species Name4","Long species Name5","Long species Name5")
Run Code Online (Sandbox Code Playgroud)
我想将数字轴和轴标题保留在原位.我还想添加一个传奇.所以最终产品看起来像这样

在不必完全清除轴的情况下,最好的方法是什么
par(xaxt = "n", yaxt = "n")
Run Code Online (Sandbox Code Playgroud)
并添加文字?谢谢你的建议.
我想将一个共同的传说集中在两个地块下面.我曾经xpd=TRUE允许在剧情本身之外进行打印,并且oma为图例创造空间.然而,传说不会在水平方向上移动并且会在"早期"垂直方向上被剪切.有什么建议?
quartz(title="PCoA",12,6)
par(mfrow=c(1,2),oma=c(5,0,0,0),xpd=TRUE)
plot(1:3,4:6,main="plot 1")
plot(1:3,4:6,main="plot 2")
# Clips the plot
legend(1,3.5,ncol=3,c("0-1 km","1-5 km","outside barrier"),fill=c("green","orange","red"), title="Fetch")
# Won't let me move the legend sideways
legend(0,3.5,ncol=3,c("0-1 km","1-5 km","outside barrier"),fill=c("green","orange","red"), title="Fetch")
Run Code Online (Sandbox Code Playgroud)

UPDATE
通过下面的解决方案,可以通过拖动边缘来改变图形的尺寸,从而切割图形的边缘(见下文).可能会发生什么想法?


是否可以使用定位器添加箭头?我试了试但没有运气....
plot(1:3)
arrows(x0=locator(1), x1=locator(1),
y0=locator(1), y1=locator(1), code=1)#single headed arrow
Run Code Online (Sandbox Code Playgroud) 是否有可能提取ggplot图形/贴图的特定部分并将它们并排放置在辅助图形中,但仍然将点添加到三个帧中,就好像它们仍然是一个图形,即对于以下地图

创建一个分为3个部分的地图,然后可以将其作为一个图形进行操作(即同时向图形的所有三个部分添加点数?

更新:可重复的例子
set.seed(1)
dfx<-c(sample(1:1000,100),sample(2000:3000,100),sample(4000:3000,100))
dfy<-c(sample(1:1000,100),sample(2000:3000,100),sample(4000:3000,100))
p<-ggplot()+
coord_fixed()+
geom_point(aes(x=dfx,y=dfy))
p
Run Code Online (Sandbox Code Playgroud)

我正在同一个ggplot中绘制geom_path对象和geom_text对象,但遇到了以下问题:
#load the data frames
df1 <- data.frame(x=c(32, 42, 52), y=c(15, 20, 25), grp=c(1, 2, 2), site=c("A", "B", "C"))
df1$grp = factor(df1$grp)
colnames(df1)[3] = "Group"
df2 <- data.frame(x=c(32, 42, 52), y=c(15, 20, 25))
#create basic plot with site name coloured by group
p = ggplot(df1, aes(x=x, y=y, label=site))
p = p + geom_text(aes(colour=factor(Group)), size=4)
p = p + coord_fixed()
#I try adding a path
p = p + geom_path(data=df2, aes(x=x, y=y))
Run Code Online (Sandbox Code Playgroud)
但是在eval(expr,envir,enclos)中得到错误Error:找不到对象'site'
有任何想法吗?
我的数据采用以下格式:
input<-data.frame(
region=c("A","T","R","R","T"),
geomorph=c("F","F","S","S","P"),
depth=c(2.6,3.5,5.8,6.7,8.9))
> input
region geomorph depth
1 A F 2.6
2 T F 3.5
3 R S 5.8
4 R S 6.7
5 T P 8.9
Run Code Online (Sandbox Code Playgroud)
我想创建一个汇总表,使得对于给定的深度类别(即0-3,3-6,6-10),区域(即A,R,T)和地貌(即F,S)的条目数量,P)计算并呈现如下:
output<-data.frame(
depth.category=c("0-3","3-6","6-10"),
total=c(1,2,2),
A=c(1,0,0),
R=c(0,1,1),
T=c(0,1,1),
F=c(1,1,0),
S=c(0,1,1),
P=c(0,0,1))
> output
depth.category total A R T F S P
1 0-3 1 1 0 0 1 0 0
2 3-6 2 0 1 1 1 1 0
3 6-10 2 0 1 1 0 1 1
Run Code Online (Sandbox Code Playgroud)
有什么建议怎么办?