datapoints shape & color

87 views
Skip to first unread message

IRich

unread,
Mar 11, 2014, 5:48:05 AM3/11/14
to ggp...@googlegroups.com
Hello,

I have this dot-plot:

dotAW<-A_W_point

dotAW<-ggplot(dotAW,aes(x=AASW, y=WW, fill=taxa))  

dotAW<-dotAW  + geom_jitter(aes(color = taxa), size = 4)

dotAW<- dotAW  +geom_abline(intercept = 0, slope = 1)

dotAW<- dotAW + scale_y_continuous(limits=c(0,0.5)) + scale_x_continuous(limits=c(0,0.5))

dotAW

ggsave(dotAW, file="dotAW.jpg", width=12, height=10)

data: http://goo.gl/8dZoAq

it's based on relative abundance of bacteria (x & y) and taxa contains a list of operational taxonomic units. I want to correlate it for different the samples (x & y, continuos values). I have so many different taxa, that it's getting confusing, that's why I want to give subset my taxa and give them different shapes, but still using a defined color vector. How can I do that?

Thanks!

Inga 



Ben Bond-Lamberty

unread,
Mar 11, 2014, 6:24:06 AM3/11/14
to ggplot2
Yes, it's confusing because you're assigning color to taxa which has
32 different values! If you want to fit and show different linear
regressions use something like
>geom_smooth( method='lm',ads(group=shape ).

Anyway, it might help the list help you to have a better understanding
of what you're trying to do.
Kind regards,
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.

IRich

unread,
Mar 11, 2014, 6:31:51 AM3/11/14
to ggp...@googlegroups.com
I don't want to do any statistical regression. What I want to visualize is which of the taxa is more abundant when doing a pairwise comparison of my groups. The abline helps for visualization

I hope that clarifies a bit
regards
Inga

Ben Bond-Lamberty

unread,
Mar 11, 2014, 12:45:05 PM3/11/14
to IRich, ggplot2
If you're committed to an x-y plot, perhaps treat the taxa as a
numeric variable, e.g.
> p <- ggplot(df,aes(x=AASW, y=WW))
> p <- p + geom_jitter(aes(color = as.numeric(taxa), shape=shape), size = 4)
> p <- p +geom_abline(intercept = 0, slope = 1)
> p <- p+ scale_y_continuous(limits=c(0,0.5)) + scale_x_continuous(limits=c(0,0.5))

It all depends on what you're trying to distinguish I guess. Could
also label the points using geom_text and plot everything on a log-log
scale?

Ben

Dennis Murphy

unread,
Mar 11, 2014, 6:02:13 PM3/11/14
to IRich, ggplot2
It's not obvious to me what you want, but if you want to group your
taxa and identify them with different shapes, here's one way you might
do it, which puts all of the groups on one plot.

ggplot(dotAW, aes(x = AASW, y = WW)) +
geom_point(aes(shape = shape),
position = position_jitter(width = 0.005, height = 0.005)) +
geom_abline(intercept = 0, slope = 1) +
scale_shape_manual(values = c(1, 0, 2),
labels = c("Group 1", "Group 2", "Group 3")) +
labs(shape = "Taxa\ngroups")

I had no idea how you wanted to name the subgroups, so I just threw
something out there; edit it to something sensible.

Another option is to facet by taxa subgroups. If you want to do this,
I'd change the values in dotAW$shape to something more relevant as the
values become the strip titles in the faceted plot.

ggplot(dotAW, aes(x = AASW, y = WW)) +
geom_point(position = position_jitter(width = 0.005, height = 0.005)) +
geom_abline(intercept = 0, slope = 1) + facet_wrap(~ shape, nrow = 1)


Dennis

IRich

unread,
Mar 13, 2014, 11:08:15 AM3/13/14
to ggp...@googlegroups.com, IRich
That looks the way I was thinking. Thank you Dennis!

Do you know there is a way how to additionally color code the groups. I thought of one color vector from color brewer. Maybe it would be to much information in one plot, but I want to try how it looks.
regards
Inga

IRich

unread,
Mar 13, 2014, 2:11:00 PM3/13/14
to ggp...@googlegroups.com, IRich
and thanks Ben as well!

I removed the geom_jitter, it changes the position of the dots in the graphs to avoid overlapping. It is not necessary for my purpose.

/Inga

Dennis Murphy

unread,
Mar 13, 2014, 5:20:07 PM3/13/14
to IRich, ggplot2
Just add a color aesthetic, but you probably want to merge the two
legends so you have to make sure they have the same breaks, labels and
legend titles.

library(ggplot2)
ggplot(dotAW, aes(x = AASW, y = WW)) +
# theme_bw() +
geom_point(aes(shape = shape, color = shape),
position = position_jitter(width = 0.005, height = 0.005)) +
geom_abline(intercept = 0, slope = 1) +
scale_color_brewer(palette = "Set1",
labels = c("Group 1", "Group 2", "Group 3")) +
scale_shape_manual(values = c(1, 0, 2),
labels = c("Group 1", "Group 2", "Group 3")) +
labs(shape = "Taxa\ngroups", color = "Taxa\ngroups")

This doesn't seem like an improvement to me; to make the colors stand
out, it's probably better to use solid shapes rather than hollow ones,
but the tradeoff is more visual clutter and obscurity in the lower
left corner of the plot.

Dennis

IRich

unread,
Mar 14, 2014, 10:43:12 AM3/14/14
to ggp...@googlegroups.com, IRich
I modified in this following way, doesn't look so bad. It's getting more and more in shape as I had in mind. The overall aim with this color and shape thing is that I would like to have an individual combination of shape and color for each taxa and show this also in the legend. thanks for the patience! I'm a bit lost with combining shape and colors.

/Inga

ggplot(dotAW, aes(x = AASW, y = WW)) + 
     theme_bw() + 
  geom_point(aes(shape = shape, color = shape), size=3,
             position = position_jitter(width = 0.002, height = 0.002))  + xlim(0,0.15) + ylim(0,0.15) +          
  geom_abline(intercept = 0, slope = 1) + 
  scale_color_brewer(palette = "Set1") 
  #labs(shape = "Taxa\ngroups", color = "Taxa\ngroups") 
                     #labels = c("Group 1", "Group 2", "Group 3")) + 
  #scale_shape_manual(values = c(1, 0, 2), 
                     #labels = c("Group 1", "Group 2", "Group 3")) + 

IRich

unread,
Mar 14, 2014, 10:54:40 AM3/14/14
to ggp...@googlegroups.com, IRich
and another thing which is important is that the combination of color and shape remains consistent. I do have different groups which I want to treat the same way. This is also the reason why some of the taxa have zero abundance in this particular plot.

/Inga

IRich

unread,
Mar 17, 2014, 3:21:47 PM3/17/14
to ggp...@googlegroups.com, IRich
can somebody help me with my last question from friday? I get two different legends when I add shape and color.

Thanks so much!
Inga

Dennis Murphy

unread,
Mar 17, 2014, 7:10:06 PM3/17/14
to IRich, ggplot2
Does this work?

ggplot(dotAW, aes(x = AASW, y = WW)) +
theme_bw() +
geom_point(aes(shape = shape, color = shape), size=3,
position = position_jitter(width = 0.005, height = 0.005)) +
xlim(-0.01, 0.15) + ylim(-0.01, 0.15) +
coord_equal() +
geom_abline(intercept = 0, slope = 1) +
scale_color_brewer(palette = "Set1",
labels = c("Group 1", "Group 2", "Group 3")) +
scale_shape_manual(values = c(16, 15, 17),
labels = c("Group 1", "Group 2", "Group 3")) +
labs(shape = "Taxa\ngroups", color = "Taxa\ngroups")


To reiterate from an earlier message, in order to merge scales, each
one has to have:
* the same legend title;
* the same breaks;
* the same labels.
Since both scales use the same aesthetic, the breaks will be the same
so they don't have to be stated explicitly; however, the labels do
have to be stated in each scale since they differ from their defaults
(the factor levels, which in this case are circle, rectangle and
triangle). Setting the two legend titles in labs() ensures the same
legend titles.

Since you use point jittering, you need to expand your x- and y-limits
on the low end to avoid having jittered points removed from the plot.
With this specification, only the two outliers are removed from the
plot (as opposed to 13 or 14 from your last code chunk).

Dennis

IRich

unread,
Apr 8, 2014, 11:38:27 AM4/8/14
to ggp...@googlegroups.com, IRich
Thanks Dennis! I needed a break from this problem, but I see what you mean and will try to adapt it 

IRich

unread,
Apr 8, 2014, 12:41:37 PM4/8/14
to ggp...@googlegroups.com
I found a way to go an to merge color and shapes in the legend by using:

guides(colour = guide_legend(override.aes = list(shape = c(16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 15, 15 ,15, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18 ,18 ))))

but the way I use it is wrong, it doesn't work

the script:
# made my own color pallete
IRPalette1 <- c("#d73027", "#f46d43", "#fdae61", "#fee090", "#ffffbf", "#e0f3f8", "#abd9e9", "#74add1", "#4575b4")

ggplot(dotAW, aes(x = AASW, y = WW)) + 
  theme_bw() + 
  geom_point(aes(shape = shape, color = co), size=3, 
             position = position_jitter(width = 0.005, height = 0.005))  + 
  xlim(-0.01, 0.15) + ylim(-0.01, 0.15) + 
  coord_equal() + 
  geom_abline(intercept = 0, slope = 1) + 
  scale_color_manual(values =IRPalette2) +                  
  scale_shape_manual(values = c(16, 15, 17, 18)) + 
  guides(colour = guide_legend(override.aes = list(shape = c(16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 15, 15 ,15, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18 ,18 ))))
 

IRich

unread,
Apr 9, 2014, 4:47:07 AM4/9/14
to ggp...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages