1

How transform asp=1 from plot in R to ggplots. theme(aspect.ratio = 1) is not the answer.

Problem: Given 3 points, A, B and C, where Distance between A - B is 50 and from C to A or B is 25. This information is stored in a matrix 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")))

In R when used regular plot, asp is needed to produce the good plot:

 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

! https://i.stack.imgur.com/VyktH.jpg "good plot"

! https://i.stack.imgur.com/WBnvy.jpg "Not Desired Plot"

When used ggplots the code is:

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))

and produces the same as the not desired plot, even when used:

phipo + theme(aspect.ratio = 1) ### makes the not desired plot.
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Could you add dput(datahipo)? for that reproducibility – Nate Jul 19 '16 at 15:18
  • Yes, I added dput for initial data (m.hipo) and for datahipo. – Edgar Gonzalez Jul 19 '16 at 15:32
  • I'll try and work on this in a bit, full time job getting in the way of my stacking... – Nate Jul 19 '16 at 15:45
  • 1
    See [here](http://stackoverflow.com/questions/13445753/force-ggplot2-scatter-plot-to-be-square-shaped) for some ideas. Mostly it looks like you'd need to set the x and y limits to be identical to get what you want. – aosmith Jul 19 '16 at 15:47
  • Nathan, Thanks I appreciate the effort. aosmith, I tried those options coord_fixex() but it displays grid.call error. Interesting but not quite the same issue. – Edgar Gonzalez Jul 19 '16 at 16:13

1 Answers1

0

Here is a work around:

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))

I tried the coord_fixed(ratio = 1) solution offered up in the answer linked by aosmith but couldn't produce the desired result. Not sure why this is, maybe graphics device setting?, anyone have any ideas?

Community
  • 1
  • 1
Nate
  • 10,361
  • 3
  • 33
  • 40