小编use*_*482的帖子

如何使用facet_wrap将yaxis设置为等于xaxis geom_point

我正在使用ggplot绘制一个相当简单的散点图.我主要想说明x轴和y轴之间的相关性.因此,我希望xaxis的限制与yaxis的限制相同.

ggplot(VV,aes(x=R1_Zsc,y=R2_Zsc)) +
  geom_point() +
  stat_smooth() +
  facet_wrap(~variable, scales="free")
Run Code Online (Sandbox Code Playgroud)

我尝试了下面的变体,但这也不起作用:

ggplot(VV, aes(x=R1_Zsc,y=R2_Zsc)) +
  geom_point() +
  stat_smooth() +
  xlim=range(c(VV$R1_Zsc,VV$R2_Zsc)) +
  ylim=range(c(VV$R1_Zsc,VV$R2_Zsc)) +
  facet_wrap(~variable, scales="free")
Run Code Online (Sandbox Code Playgroud)

我已经制作了一个数据框,其中包含每个变量的xlimits和y限制,并且我认为我可以使用它,但我不确定如何.

df_rng <- ddply(VV, .(variable), summarise, min=range(c(R1_Zsc,R2_Zsc))[1],max=range(c(R1_Zsc,R2_Zsc))[2])
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

谢谢,coord_fixed(ratio=1)似乎没有工作.我想设置xlimylim相同的值.

这是输出图 在此输入图像描述

cars数据集中的示例生成以下图表:

在此输入图像描述

r ggplot2

3
推荐指数
1
解决办法
1500
查看次数

R - 将相关矩阵转换为成对矩阵

我想将相关矩阵转换为成对表(删除自匹配和重复项)。

这是一个示例数据集。

rmat.test<-structure(c(1, 0.861194908618927, 0.826931774616241, 0.796892821788788, 
0.83096307516098, 0.861194908618927, 1, 0.878752708435059, 0.855243384838104, 
0.880544185638428, 0.826931774616241, 0.878752708435059, 1, 0.850607931613922, 
0.850719928741455, 0.796892821788788, 0.855243384838104, 0.850607931613922, 
1, 0.876053333282471, 0.83096307516098, 0.880544185638428, 0.850719928741455, 
0.876053333282471, 1), .Dim = c(5L, 5L), .Dimnames = list(c("A", 
"B", "C", "D", "E"), c("A", "B", "C", "D", "E")))
Run Code Online (Sandbox Code Playgroud)

来自上一篇文章的答案。我有以下代码。

df.corr.pw<-reshape2::melt( cbind(
  V1=rownames(rmat.test), 
  as.data.frame(rmat.test))
)
df.corr.pw<-subset(df.corr.pw,value!=1)
Run Code Online (Sandbox Code Playgroud)

但是,我无法找出删除重复条目的有效方法(即 AB 的第 2 行,BA 的第 6 行)。

> df.corr.pw
   V1 variable     value
2   B        A 0.8611949
3   C        A 0.8269318
4   D        A 0.7968928
5   E        A 0.8309631
6 …
Run Code Online (Sandbox Code Playgroud)

r matrix

2
推荐指数
1
解决办法
2473
查看次数

R移动矢量

我有一个数据框,我想'对齐'每列,以便每列的最大值在同一行.

我试图使用基本功能来做到这一点,但是得到了错误的结果,即.只是覆盖而不是转移.我刚刚在Hmisc中找到了Lag函数,但是,我确信在base中有一种方法可以做到这一点我只是想错了.我更喜欢这个,就像我以后尝试在另一台计算机上运行它一样R的不同版本总是有一些不受支持的包.

谢谢你的帮助,

maxIndices<-apply(df,2,function(x){
maxInt<-max(x,na.rm=T)
maxInt_indx<-which(x==maxInt) 
})
maxMaxIndex<-max(maxIndices)
minMaxIndex<-min(maxIndices)
##
apply(df,2,function(x){
  maxInt<-max(x,na.rm=T)
  maxInt_indx<-which(x==maxInt)
 shift<-maxMaxIndex-maxInt_indx
shifted_vec<-c(rep(NA,times=shift), x[1:length(x)+shift]) ## this is producing the wrong results
# shifted_vec<-Lag(x,shift) # is there a way to do this using just base functionality
})
Run Code Online (Sandbox Code Playgroud)

r

1
推荐指数
1
解决办法
9509
查看次数

ggplot2 为什么我的 geom_text 字体看起来这么糟糕

我正在用 ggplot2 制作条形图。由于某种原因,使用 geom_text 创建的字体看起来与其他字体非常不同。它看起来“更糟”,有点像素化,当我将 pdf 粘贴到 ppt 演示文稿中时,你几乎无法阅读 geom_text 文本。轴字体看起来很棒。
任何帮助表示赞赏,

  ggplot(OECD_CDNpercap_tot_aveplsGDP_select, aes(x=Country,mean, y=mean, 
                                                  label=paste(round(mean),"(",round(mean_GDPP),"% )"))) +
    geom_col(stat='identity', aes(fill=Can))  +
    geom_hline(yintercept=G20_mean,linetype="dashed")+
    geom_hline(yintercept=G7_mean,linetype="dashed")+
    scale_fill_manual(values=c("red","grey")) + 
    geom_text(color="black",size=4,hjust=-0.05)+ #hjust+
    geom_text(aes(0,G20_mean,label =paste("G20 ave",round(G20_mean))), size=4,vjust=-1,hjust=-0.22)+
    geom_text(aes(0,G7_mean,label =paste("G7 ave",round(G7_mean))), size=4,vjust=-5,hjust=-0.2)+
    labs(fill="Country")+
    ylab("per capita spending CDN$ (% of GDP)")+
    ylim(0,17000)+
    xlab("")+
    coord_flip()+
    guides(fill=FALSE)+
    theme_bw()+
    theme(axis.text = element_text(size = 12),axis.title = element_text(size = 12))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果你愿意的话,这是我的数据。

    dput(OECD_CDNpercap_tot_aveplsGDP_select)
structure(list(Country = structure(1:20, .Label = c("IND", "IDN", 
"CHN", "ZAF", "MEX", "TUR", "BRA", "RUS", "KOR", "ITA", "GBR", 
"JPN", "AUS", "FRA", "CAN", "AUT", …
Run Code Online (Sandbox Code Playgroud)

r ggplot2

1
推荐指数
1
解决办法
520
查看次数

标签 统计

r ×4

ggplot2 ×2

matrix ×1