How do I make a dot plot with a continuous x-axis (ggplot2)?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How do I make a dot plot with a continuous x-axis (ggplot2)?



I'm trying to create a vertically oriented double plot with a line plot above and dot plot below, with both on the same (continuous, date) x-axis. I've successfully placed the two plots on a common axis and finished the (upper) line plot, but when I try to change the (lower) dot plot's x-axis from categorical to continuous, all my dots bunch up in the middle of the plot.



I only include here my code for the dot plot for simplicity, but if it turns out I need to show you the full double plot, I can do that.



Here's a small subset of my data, then my code, as far as I've gotten with it:


data <- structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L
), .Label = c("11/11/2016", "12/16/2016", "12/2/2016", "12/23/2016"
), class = "factor"), factor = c(2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L
), temp = c(-19.85, -19.94, -20.77, -21.3, -21.71, -21.88, -22.03,
-22.74, -22.86, -18.88, -19.02, -19.22, -19.32, -19.32, -19.55,
-19.68, -20.23, -20.32, -21.37, -16.63, -19.01, -19.67, -20.47,
-21.14, -21.23, -23.01, -24.43, -24.61, -24.76, -15.9, -18.87,
-19.02, -19.16, -19.44, -19.62, -22.38, -24.37, -24.92, -26.9
)), .Names = c("date", "factor", "temp"), class = "data.frame", row.names = c(NA,
-39L))


library(ggplot2)
library(scales)


#format date and order date levels (the second line here gives me a warning, but seems to do what I want it to)..
data$date <- as.Date(data$date, "%m/%d/%Y")
data$date.chr <- factor(data$date, as.character(data$date))
data$date.chr <- as.Date(data$date.chr)


#now plot..
ggplot(data, aes(x = date.chr, fill = factor(factor), y = temp)) +
geom_dotplot(binaxis = 'y', stackdir = 'center', method = 'histodot', binwidth = 0.3, position=position_dodge(0.8)) +
scale_x_date(date_breaks = "2 weeks", labels = date_format("%e %b"), limits = as.Date(c("2016-11-04","2016-12-23"))) +
labs(title="", x="", y="response temp (°C)") +
theme_minimal() +
theme(axis.title.y = element_text(vjust=1)) +
theme(legend.position="top") +
guides(fill = guide_legend(override.aes = list(size=10)))



(My session info:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1)



Any suggestions how I can (dot) plot this data on a continuous x-axis? (again, so I can line it up with the date axis in a plot above it)





scale_x_date is a continuous scale. Or do you mean you need to actually use scale_x_continuous?
– Axeman
Mar 21 '17 at 16:48


scale_x_date


scale_x_continuous





Axeman: I don't know, but am open to suggestions. The code above does create a continuous scale, but doesn't plot the data in the way I'm aiming for.
– Joshua
Mar 21 '17 at 19:58




1 Answer
1



I'm not sure if this is what you are looking for, but let's see:


data$date <- as.Date(data$date, "%m/%d/%Y")
data$date.chr <- factor(data$date)
#create dummy variable to get both the position and "filling" right
data$datefact <- paste(data$factor,data$date.chr)



The trick here is to set the "group" argument in geom_dotplot to the dummy variable created before:


geom_dotplot


ggplot(data, aes(x = date, y = temp)) +
# geom_point() +
geom_dotplot(aes(x = date, group = datefact, fill = factor(factor)),binaxis = 'y',
stackdir = 'center',
method = 'histodot',
binwidth = 0.3)+
scale_x_date(date_breaks = "2 weeks", labels = date_format("%e %b"), limits = as.Date(c("2016-11-04","2016-12-23"))) +
labs(title="", x="", y="response temp (°C)") +
theme_minimal() +
theme(axis.title.y = element_text(vjust=1)) +
theme(legend.position="top") +
guides(fill = guide_legend(override.aes = list(size=10)))



giving:



enter image description here



Is this what you wanted ?





LoBu: This helps.. but there's still something I don't quite understand yet going on here. When I tried this on my full dataset, including the factor made the dot plot groupings too complicated to look at. When I omitted the factor and just used "date" in the grouping, the placement of the dots within date groupings changed visibly and become symmetrical (which looks more right to me). I'm not quite sure what setting(s) of the dot plot is/are affected here. I think if you tried this on the small example here, the dots would become symmetrical. Any idea what is going on here?
– Joshua
Mar 21 '17 at 21:48





The placement of the dots "moved", I think, because if you change the grouping the "distribution statistics" used to make the dotplot change. That, in addition to position_dodge led probably to the asymmetrical plots. Just removing position_dodge gives the plot in the edited answer. Is that ok ? Consider that if you keep only date in the grouping, the dotplot will not take into account the differences between your factor levels distributions.
– lbusett
Mar 21 '17 at 22:23



position_dodge


position_dodge


date





LoBu: Right! position_dodge was mistakenly carried over from a previous code where I had a different grouping structure. In any case, once I tweaked the dodge value, I got the groups separated enough to see them clearly. Thank you for your help.. I'm still learning this language.
– Joshua
Mar 22 '17 at 14:15


position_dodge





you're welcome. It was an interesting problem.
– lbusett
Mar 22 '17 at 18:16





I didn't realize I should click the checkmark to accept your answer. Just did that..
– Joshua
Jun 14 '17 at 16:58






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered