0

This question pertains to the second type of ggplot which does not request reshaping to longer data frames. Reshaping to a longer data frame isn't easily done in this case due to the memory requirements.

Only answers that begin with ggplot(df) will be accepted. If you do not wish to follow the ggplot(df) manner then please ignore this question and move on.

enter image description here

df=data.frame(xx=runif(10),yy=runif(10),zz=runif(10))
require(ggplot2)
ggplot(df) + 
    geom_line(aes(xx,yy, color='yy'))+
    geom_point(aes(xx,yy, color='yy'))+
    geom_line(aes(xx,zz, color='zz'))+
    geom_point(aes(xx,zz, color='zz'))+
    ggtitle("Title")

Is there a way to create a geom_both function that works in the ggplot manner?

This does not work: geom_both=function(...) { geom_line(...)+geom_point(...) }

Chris
  • 1,219
  • 2
  • 11
  • 21
  • 4
    You could create a new geom if that's worth the effort, but it really does seem like there are better methods that fit better with ggplot's paradigm. If your data is too large to reshape for plotting, maybe you'd be better off summarizing it anyway before plotting it. Where does this text come from? – camille Dec 21 '19 at 16:39
  • 2
    I wonder what type of data you have that reshaping it is too memory intense. I am not sure if the quotes around your color argument are intended - but it will not result in colouring based on the y/z values. I am not sure what you actually want to achieve, but you seem to be looking for something like a 'type b' plot : https://stackoverflow.com/q/38895565/7941188 Look especially at @teunbrand 's answer - he is providing the code for a new geom – tjebo Dec 21 '19 at 16:40
  • 2
    Does this answer your question? [Is it possible to achieve base-r plot \`type=b\` functionality in ggplot2?](https://stackoverflow.com/questions/38895565/is-it-possible-to-achieve-base-r-plot-type-b-functionality-in-ggplot2) – Rémi Coulaud Dec 21 '19 at 17:12
  • @camille -- the text comes from the help text for `ggplot2:ggplot`. I was surprised to see it there, since the consistent message I've gotten from ggplot and tidyverse tutorials is that wide dataframes should almost always be reshaped to longer form to assist with plotting. I would think there must be some way to filter the data or summarize it, or use `data.table::melt` to reshape it within whatever memory constraints there are. – Jon Spring Dec 21 '19 at 17:44

1 Answers1

2

I believe this does what you asked

library(ggplot2)
library(lemon) ## contains geom_pointline

df=data.frame(xx=runif(10),yy=runif(10),zz=runif(10))
ggplot(df) + 
  geom_pointline(aes(xx,yy, color='yy'))+
  geom_pointline(aes(xx,zz, color='zz'))+
  ggtitle("Title")

enter image description here

To eliminate the gap between the lines and the points, you can add distance=0 like this:

ggplot(df) + 
  geom_pointline(aes(xx,yy, color='yy'), distance=0)+
  geom_pointline(aes(xx,zz, color='zz'), distance=0)+
  ggtitle("Title")

enter image description here

EDIT: Another option is to define a function like this

add_line_points = function(g, ...){
  gg = g + geom_point(...) + geom_line(...)
  return(gg)
}

and use %>% instead of +

ggplot(df) %>% ## use pipe operator, not plus
  add_line_points(aes(xx,yy, color='yy')) %>%
  add_line_points(aes(xx,zz, color='zz'))

Note: I adapted this from here.

bmacGTPM
  • 577
  • 3
  • 12