R novice trying to generate a figure for a publication

61 views
Skip to first unread message

Mike Furgason

unread,
Nov 11, 2014, 3:19:32 PM11/11/14
to ggp...@googlegroups.com
Given a tab delimited file with multiple y values arranged in columns for given x(first row), I am trying to create a graph similar to figure 6 in this paper: https://paperpile.com/shared/na1vC5.  I am new to R, could anyone give me a basic script that could do this or at least get me started?  Thanks

Ben Bond-Lamberty

unread,
Nov 11, 2014, 8:09:07 PM11/11/14
to ggplot2
1) The PDF at that link isn't loading for me. Please don't make
everyone click through and hunt down the figure; better to copy it as
a small graphic and attach to your question, or just describe it.

2) You're posting this question to the ggplot2 listserv; I assume this
is what you want to use to make your plot? R has a number of plotting
packages.

3) If yes to #2, you're basically going to want to do the following
steps: import the data into R; transform it from 'wide' (multiple y
values per row) to 'long' (one y value per row); and plot.

4) Please read the link about reproducible examples that's given at
the bottom of all emails on this listserv.

5) In the spirit of reproducible examples, here's one that shows the
steps in #3 above:

df <- data.frame(x=1:2,group1=3:4,group2=5:6) # sample 'wide' data
library(reshape2)
df_long <- melt(df, id.vars=1) # melt to 'long' format
library(ggplot2)
ggplot(df_long, aes(x,value))+geom_line(aes(color=variable)) # plot it

Ben
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Mike Furgason

unread,
Nov 12, 2014, 11:13:10 AM11/12/14
to ggp...@googlegroups.com
Image
Figure6.png

Ben Bond-Lamberty

unread,
Nov 12, 2014, 12:00:02 PM11/12/14
to ggplot2
Thanks for the image. Note that since you (again) didn't take the
trouble to provide a reproducible example or sample data, the
impression you're sending is that you expect people on this list to do
all your work for you. Which is not a great way to get help. However,
because I happened to have some time this morning, here's something to
get you started--hope it is useful.


library(ggplot2)
theme_set(theme_bw())

# Create sample data
df <- data.frame(obs=1:20,group=LETTERS[1:10], shading=1:2,
known=c(rep(T,10),rep(F,10)),score=runif(20))

# Set up the basic plot
p <- ggplot(df, aes(x=group, y=score))
p <- p + geom_blank() # not sure why I get an error without including this this

# Add background shading, alternating between groups
p <- p + geom_rect(aes(xmin=as.numeric(group)-0.5,xmax=as.numeric(group)+0.5,ymin=-Inf,ymax=Inf,fill=factor(shading)),
alpha=0.2)

# Add the points
p <- p + geom_point(aes(color=known))

# Fine-tune: custom colors, horizontal line, etc.
p <- p + scale_fill_grey(guide=F)
p <- p + scale_color_manual("", values=c("green", "yellow")) # point colors
p <- p + geom_hline(yintercept=0.5)

# Display and save the figure
print(p)
ggsave("sample.pdf")

Ben
sample.pdf

Brandon Hurr

unread,
Nov 12, 2014, 1:29:59 PM11/12/14
to Ben Bond-Lamberty, ggplot2
Additionally, it looks like the points in the original are jittered. Perhaps use geom_jitter() instead of geom_point(). Otherwise, great work Ben. 

Mike Furgason

unread,
Nov 12, 2014, 4:47:39 PM11/12/14
to ggp...@googlegroups.com
Ben, sorry, if that is the impression that I am giving, it is not my intention. The problem is that I do not have an idea what functions to use.  Fortunately someone PM'd me earlier today with basically the info that you just posted, so I have been integrating it into my current bare-bones script, which basically only loaded the data, which is why I (again) didn't take the trouble to post it.  It is nice to see so many people willing to help out and I will post something more in depth once have it.
Reply all
Reply to author
Forward
0 new messages