使用cmdscale将asp = 1从Plot转换为

时间:2016-07-19 15:02:12

标签: r plot ggplot2 transform

如何从R中的绘图转换asp = 1到ggplots。主题(aspect.ratio = 1)不是答案。

问题:给定3个点A,B和C,其中A - B之间的距离为50,从C到A或B为25.这个信息存储在矩阵m.hipo中。

dput(m.hipo)
structure(c(0L, 50L, 25L, 50L, 0L, 25L, 25L, 25L, 0L), .Dim = c(3L, 
3L), .Dimnames = list(c("A", "B", "C"), c("A", "B", "C")))

在R中使用常规情节时,需要asp来产生好的情节:

 fit.hipo <- cmdscale(m.hipo, eig = TRUE, k = 2)
 x.hipo <- fit.hipo$points[, 1]
 y.hipo <- fit.hipo$points[, 2]
 plot(x.hipo,y.hipo,asp=1) ### Makes the good plot because of asp=1
 plot(x.hipo,y.hipo) ### Makes not the desired plot

http://imgur.com/9wENc7I“好情节”

http://imgur.com/iGumf4b“不想要的情节”

使用ggplots时,代码为:

datahipo<-data.frame(x.hipo,y.hipo)

dput(datahipo)
structure(list(x.hipo = c(25, -25, -1.13686837721616e-15), y.hipo = c(5.84003863998217e-07, 
5.84003863998217e-07, 2.65574210060648e-23)), .Names = c("x.hipo", 
"y.hipo"), row.names = c("A", "B", "C"), class = "data.frame")



phipo<-ggplot(datahipo, aes(x.hipo,y.hipo)) +
  geom_point(color = 'red') +
  geom_text_repel(aes(label = row.names(dist.hipo)),size=8) +
  theme_classic(base_size = 24) +
  theme(axis.text=element_text(size=24),axis.title=element_text(size=24,face="bold")) +  
  labs(list(title = "MDS for Control Proteins (Sequence)", x = First Dimension", y = "Second Dimension")) + 
  theme(axis.line.x = element_line(color="black", size = 1), axis.line.y = element_line(color="black", size = 1))

并生成与不需要的绘图相同的内容,即使使用时也是如此:

phipo + theme(aspect.ratio = 1) ### makes the not desired plot.

1 个答案:

答案 0 :(得分:0)

这是一个解决方法:

lim_var <- max(datahipo)

ggplot(datahipo, aes(x.hipo,y.hipo)) +
  geom_point(color = 'red') +
  coord_fixed(xlim = c(-lim_var, lim_var), ylim =c(-lim_var, lim_var))

我尝试了由{ammmith链接的answer中提供的coord_fixed(ratio = 1)解决方案,但无法产生预期效果。不知道为什么会这样,也许图形设备设置?,任何人都有任何想法?