geom_dotplot: 4 data groups, centred, side by side, different shapes (?)

588 views
Skip to first unread message

Martin Kärcher

unread,
Nov 6, 2012, 11:32:27 AM11/6/12
to ggplot2
Dear all,
 
Currently, I have plotted my data using geom_line, geom_smooth and geom_point. However, as you will see the result is suboptimal. A friend of mine helped me to use geom_dotplot instead of geom_point but we both could not solve the problem. Ideally, I would like to have the dots arranged next to each other (group 1, 2, 3, 4 from left to right) and centred on the y-axis at 1-2 or 2-3, respectively. They do not necessarily have to be centred, if this is not possible, but as it is (geom_point and position_dodge) it looks a bit messy. Ideally, I would like to maintain the shapes of the various groups which we did not manage in geom_dotplot. Below, I have pasted in the code which I also attached as an R script. I have also attached the PDFs of a) the original graph which one of the reviewers criticised (…), b) the version using geom_point and position_dodge, and c) a version using geom_dotplot. I would be very grateful if someone could help me with this problem, please.

Best wishes,

Martin

rm(list=ls())

library(proto)
library(grid)
library(reshape)
library(plyr)
library(digest)
library(ggplot2)
library(tools)

# setwd("C:\\Users\\mhk\\Documents\\nur.fueR\\ggsave\\")

# 3 data frames used in the graph:

df.perc.3 <- structure(list(trt = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
    3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("0-1",
    "1-2", "2-3")), resp = c(NA, 92.6096997690531, 79.7921478060046,
    NA, 98.7080103359173, 94.9181739879414, NA, 97.1326164874552,
    96.057347670251, NA, 98.4375, 97.94921875, NA, 97.8982300884956,
    97.8982300884956, NA, NA, NA, NA, 90.686274509804, 90.4411764705882,
    NA, 97.5510204081633, 93.7414965986395, NA, 88.2820258192651,
    83.8133068520358, NA, 95.6653225806452, 94.6572580645161, NA,
    96.5116279069768, 94.670542635659, NA, 98.2581967213115, 97.438524590164
    ), group = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
    1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 4L, 4L, 4L, 3L, 3L, 3L, 4L, 4L,
    4L, 3L, 3L, 3L, 4L, 4L, 4L, 3L, 3L, 3L), .Label = c("carnica EW", "carnica ED",
    "mellifera EW", "mellifera ED"), class = "factor")), .Names = c("trt", "resp",
    "group"), row.names = c(NA, -36L), class = "data.frame")

df.perc.2b <- structure(list(trt = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("0-1", "1-2", "2-3")),
    resp = c(NA, 94.0193416337148, 90.4454585871723,
        NA, 97.865467535666, 95.8563947267053),
    group = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("eggs in worker cells (EW)", "eggs in drone cells (ED)"), class = "factor"),
    se = c(NA, 1.68994661360938, 2.95728999211006,
        NA, 0.549126551165786, 0.824844616926236)),
    .Names = c("trt", "resp", "group", "se"), row.names = c(NA, -6L), class = "data.frame")

df.perc.2a <- structure(list(trt = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("0-1", "1-2", "2-3")),
    resp = c(100, 94.0193416337148, NA,
        100, 97.865467535666, NA),
    group = structure(c(2L, 2L, 2L, 1L, 1L, 1L), .Label = c("eggs in worker cells (EW)", "eggs in drone cells (ED)"), class = "factor"),
    se = c(NA, NA, NA,
        NA, NA, NA)),
    .Names = c("trt", "resp", "group", "se"), row.names = c(NA, -6L), class = "data.frame")


# The version of the graph using geom_point and position_dodge:

ggplot() +
    geom_line(data = df.perc.2a, aes(y = resp, x = trt, group = group, linetype = group), stat = "identity",
        size = 0.5, colour = "black") +
    geom_smooth(data = df.perc.2b, aes(y = resp, x = trt, group = group, linetype = group, ymax = resp + se,
        ymin = resp - se), stat = "identity", size = 0.5, colour = "black") +
    geom_point(data = df.perc.3, aes(y = resp, x = trt, group = group, fill = group, shape = group),
        position = position_dodge(width=0.2, height=0), stat = "identity", size = 2) +
    scale_fill_manual(value = rep(c("white", "black"), each = 2)) +
    scale_shape_manual(value = rep(c(21, 23), 2)) +
    scale_x_discrete(expand = c(0, 0.2), name = "age range of eggs (day)") +
    scale_y_continuous(expand = c(0.02, 0), breaks = seq(80, 100, 5), name = "eggs remaining (%)") +
    opts(legend.position = c(0.395, 0.37), legend.background = theme_rect(colour = 0),
        legend.key = theme_rect(colour = 0), legend.key.size = unit(c(0.9, 0.9), "lines"),
        legend.title = theme_blank(), legend.text = theme_text(size = 9)) +
    opts(panel.grid.major = theme_line(colour = "white", size = 0.5)) +
    opts(axis.text.x = theme_text(size = 9, vjust = 0.9),
        axis.text.y = theme_text(size = 9, hjust = 0.9)) +
    opts(axis.title.x = theme_text(size = 11, vjust = 0.61, hjust = 0.6),
        axis.title.y = theme_text(size = 11, hjust = 0.555, vjust = 0.59, angle = 90)) +
    opts(plot.margin = unit(c(0.3, 0.3, -0.03, -0.23), "lines"))


# ggsave("A.plot13.pdf", width = 85/25.4, height = 85/25.4, family = "sans")


# The graph a friend a mine created using geom_dotplot:

ggplot() +
    geom_line(data = df.perc.2a, aes(y = resp, x = trt, group = group, linetype = group), stat = "identity",
        size = 0.5, colour = "black") +
    geom_smooth(data = df.perc.2b, aes(y = resp, x = trt, group = group, linetype = group, ymax = resp + se,
        ymin = resp - se), stat = "identity", size = 0.5, colour = "black") +
    geom_dotplot(data = na.omit(df.perc.3), aes(y = resp, x = factor(trt), shape = group, fill = group),
        stackdir = "center", size = 2, stackgroups = TRUE, method = "histodot", binaxis = "y", binpositions = "all") +
    opts(legend.text = theme_text(size = 5), legend.key.size = unit(c(0.9, 0.9), "lines"),
        legend.title = theme_blank())

# We did not manage to arrange the dots next to each other, centre them, and maintain the various shapes. This graph was made with
# an up-to-date version of R. The other graph where I use geom_point and position_dodge was made in an older version of R.

# I would be very grateful if someone could help me with this problem, please.
graph_1_preliminary versions
Figure 1.pdf
A.plot13.pdf
Rplot05.pdf

Winston Chang

unread,
Nov 6, 2012, 1:06:38 PM11/6/12
to Martin Kärcher, ggplot2
You can dodge groups of dotplots. See the second example from the bottom here:

As for shapes, geom_dotplot presently only draws circles. There was a discussion about adding different shapes a while back, but it hasn't been implemented.

-Winston


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

martin_kaercher

unread,
Dec 10, 2012, 2:12:14 PM12/10/12
to ggp...@googlegroups.com, Martin Kärcher

Dear Winston,

A while ago I have replied to your helpful reply via my yahoo account but obviously you did not receive it. Therefore, I'm now replying via the Google group below:

Dear Winston,

Thanks for your fast reply and for your help. I apologise for my late reply. I see. This means I can only centre data within one group but I cannot group the dots of 4 groups and then centre them. What I was looking for would have been a combination of “# grouping on x and g, dodging” and “# Stacking groups, using histodot, along y axis”, the last two examples on “https://github.com/wch/ggplot2/wiki/geom_dotplot”. After trying various methods, such as ordering my data in different ways, I came to the conclusion that centring vertical and dodging data is not possible when there is more than one break on the x-axis (e.g. A, B, and C from the example in github). Please correct me if I’m wrong. Below I have pasted in the original graph, the graph using geom_point, and the graph using geom_dodge into this message including the code for the graph using geom_dodge. This is the best compromise I achieved, but I would be grateful for any suggestions.

In the new version of ggplot2 several behaviours of the legends are quite odd. I fixed most of them; however, I would prefer to have the two legends aligned on the left, as in A.plot13. (One way to do this would be to change the width of legend.background for each legend or to position each legend separately, but I have no idea how to do this.) Also, I would like to move the two legends closer together, i.e. the gap between the legend above and below should be smaller. But after investing quite a lot of time into these and other R problems I realised that for an R beginner like me this is not doable. If anyone could help me improve this graph I would be very grateful. Below I have pasted in the code of the graph where I highlighted the legend background in red. Ideally, I would like to be able to move the two legends separately.

I have also pasted in a graph where I marked the border of the legends in red and one illustrating where I would like to place them.

Best wishes,

Martin

Figure 1:

A.plot13:

A.plot18:

A.plot18.red_frames:

A.plot18.aim:


R code:


rm(list=ls())

 

library(proto)

library(grid)

library(plyr)

library(reshape)

library(digest)

library(ggplot2)

library(tools)

 

setwd("C:\\Users\\mhk\\Documents\\nur.fueR\\ggsave\\")

 

df.perc.3 <- structure(list(trt = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L,

ggplot() +

                    geom_line(data = df.perc.2a, aes(y = resp, x = trt, group = group, linetype = group), stat = "identity",

                                       size = 0.5, colour = "black") +

                    geom_smooth(data = df.perc.2b, aes(y = resp, x = trt, group = group, linetype = group, ymax = resp + se,

                                       ymin = resp - se), stat = "identity", size = 0.5, colour = "black") +

                    geom_dotplot(data = na.omit(df.perc.3), aes(y = resp, x = factor(trt), fill = group, colour = group, width = 0.4),

                                       position = "dodge", binaxis = "y", stackdir = "center", dotsize = 1, binwidth = 0.5) +

                    scale_fill_manual(values = c("white", "grey82", "black", "grey40")) +

                    scale_colour_manual(values = rep("black", 4)) +

                    scale_x_discrete(expand = c(0, 0.2), name = "age range of eggs (day)") +

                    scale_y_continuous(expand = c(0.02, 0), breaks = seq(80, 100, 5), name = "eggs remaining (%)") +

                    theme(legend.position = c(0.6, 0.29), legend.background = element_blank(),

                                       legend.key = element_blank(), legend.key.size = unit(c(0.9, 0.9), "lines"),

                                       legend.title = element_blank(), legend.text = element_text(size = 9)) +

                    theme(panel.grid.major = element_line(colour = "white", size = 0.5),

                                       axis.text.x = element_text(size = 9, vjust = 0.9),

                                       axis.text.y = element_text(size = 9, hjust = 0.9),

                                       axis.title.x = element_text(size = 11, vjust = 0.61, hjust = 0.6),

                                       axis.title.y = element_text(size = 11, hjust = 0.555, vjust = 0.59, angle = 90),

                                       plot.margin = unit(c(0.3, 0.3, -0.03, -0.23), "lines")) +

                    guides(group = guide_legend(order = 2), fill = guide_legend(order = 1), colour = guide_legend(order = 1))

 

ggsave("A.plot18.pdf", width = 85/25.4, height = 85/25.4, family = "sans")

 

 

# legend.background highlighted in red:

 

ggplot() +

                    geom_line(data = df.perc.2a, aes(y = resp, x = trt, group = group, linetype = group), stat = "identity",

                                       size = 0.5, colour = "black") +

                    geom_smooth(data = df.perc.2b, aes(y = resp, x = trt, group = group, linetype = group, ymax = resp + se,

                                       ymin = resp - se), stat = "identity", size = 0.5, colour = "black") +

                    geom_dotplot(data = na.omit(df.perc.3), aes(y = resp, x = factor(trt), fill = group, colour = group, width = 0.4),

                                       position = "dodge", binaxis = "y", stackdir = "center", dotsize = 1, binwidth = 0.5) +

                    scale_fill_manual(values = c("white", "grey82", "black", "grey40")) +

                    scale_colour_manual(values = rep("black", 4)) +

                    scale_x_discrete(expand = c(0, 0.2), name = "age range of eggs (day)") +

                    scale_y_continuous(expand = c(0.02, 0), breaks = seq(80, 100, 5), name = "eggs remaining (%)") +

                    theme(legend.position = c(0.6, 0.29), legend.background = element_rect(colour = "red"),

                                       legend.key = element_blank(), legend.key.size = unit(c(0.9, 0.9), "lines"),

                                       legend.title = element_blank(), legend.text = element_text(size = 9)) +

                    theme(panel.grid.major = element_line(colour = "white", size = 0.5),

                                       axis.text.x = element_text(size = 9, vjust = 0.9),

                                       axis.text.y = element_text(size = 9, hjust = 0.9),

                                       axis.title.x = element_text(size = 11, vjust = 0.61, hjust = 0.6),

                                       axis.title.y = element_text(size = 11, hjust = 0.555, vjust = 0.59, angle = 90),

                                       plot.margin = unit(c(0.3, 0.3, -0.03, -0.23), "lines")) +

                    guides(group = guide_legend(order = 2), fill = guide_legend(order = 1), colour = guide_legend(order = 1))

 

ggsave("A.plot18.red_frames.pdf", width = 85/25.4, height = 85/25.4, family = "sans")


# p.s.: I am sorry that I could not attach the r-script and the graphs as attachments but when I tried sending them Google always gave me an error message.

martin_kaercher

unread,
Dec 10, 2012, 2:16:10 PM12/10/12
to ggp...@googlegroups.com, Martin Kärcher
Oh no! Google has simply deleted all figures. I will send this message in a similar version as a new discussion topic. Fingers crossed it works.

Cheers,

Martin
Reply all
Reply to author
Forward
0 new messages