I'm new in R and I'm struggling with some plotting in ggplot.
I have some monthly data I simply plotted as points connected with lines.
ggplot(data=df, aes(x=x,y=y)) +
geom_line(aes(group=g)) + geom_point()
Now, I'd like to add pairwise results of Wilcoxon tests between the three categories grouped.
It should look like this.
I'm a bit confused, I know stat_pvalue_manual works with categories, but I have a continuous y axis. and it should be horizontal.
Maybe there are more functions to do this.
does anyone have an example of how this could be done?
Thanks in advance.
structure(list(x = c("April", "April", "April", "May", "May",
"May", "June", "June", "June", "July", "July", "July", "August",
"August", "August", "September", "September", "September", "October",
"October", "October", "November", "November", "November", "December",
"December", "December", "January", "January", "January", "February",
"February", "February"), g = c("a", "b", "c", "a", "b", "c",
"a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a",
"b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b",
"c"), y = c(4.748, 5.3388, 5.7433, 4.744, 5.4938, 6.1583, 4.767,
5.6, 6.2067, 4.889, 5.8363, 6.295, 4.887, 5.6413, 6.15, 4.94,
5.73, 6.1833, 4.974, 5.2113, 5.77, 5.022, 5.47, 5.9117, 4.964,
5.3425, 5.7217, 4.95, 5.15, 5.9833, 4.75, 5.425, 5.7833)), class = "data.frame", row.names = c(NA, -33L))
There's a few things that make this fiddly, the main ones being that you have a discrete scale for your x-axis, and stat_pvalue_manual seems to only work with continuous scales, and a coordinate swap is needed. As a result the factor needs to be ordered, and changed from geom_line to geom_path, and the means for each factor level need to be calculated and added into the stat_test object. This results in:
#Test data
df <- structure(list(x = c("April", "April", "April", "May", "May", "May", "June", "June", "June", "July", "July", "July", "August", "August", "August", "September", "September", "September", "October", "October", "October", "November", "November", "November", "December", "December", "December", "January", "January", "January", "February", "February", "February"), g = c("a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c", "a", "b", "c"), y = c(4.748, 5.3388, 5.7433, 4.744, 5.4938, 6.1583, 4.767, 5.6, 6.2067, 4.889, 5.8363, 6.295, 4.887, 5.6413, 6.15, 4.94, 5.73, 6.1833, 4.974, 5.2113, 5.77, 5.022, 5.47, 5.9117, 4.964, 5.3425, 5.7217, 4.95, 5.15, 5.9833, 4.75, 5.425, 5.7833)), class = "data.frame", row.names = c(NA, -33L))
df$x <- factor(df$x, levels=unique(df$x))
stat.test <- compare_means(
y ~ g, data = df
)
#Calculate mean values by group
means <- aggregate(df$y, list(g=df$g), mean)
means2 <- means$x
names(means2) <- means$g
stat.test$group1 <- means2[stat.test$group1]
stat.test$group2 <- means2[stat.test$group2]
stat.test$y.position = c(13, 13.5, 13) #arbitrary location for plotting brackets
#Modify the plot
ggplot(data=df, aes(x=y,y=as.numeric(x))) +
geom_path(aes(group=g)) +
geom_point() +
stat_pvalue_manual(stat.test, coord.flip = TRUE) + coord_flip() +
scale_y_continuous("Month", labels=levels(df$x),
breaks=seq_along(levels(df$x)), minor_breaks = 1)
I've been around the forums looking for a solution to my issue but can't seem to find anything. Derivatives of my question and their answer haven't really helped either. My data has four columns, one for Year and one for Month). I've been wanting to plot the data all in one graph without using any facets for years in ggplot. This is what I've been struggling with so far with:
df<-data.frame(Month = rep(c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "February", "March"),each = 20),
Year = rep(c("2018", "2019"), times = c(220, 40)),
Type = rep(c("C", "T"), 260),
Value = runif(260, min = 10, max = 55))
df$Month<-ordered(df$Month, month.name)
df$Year<-ordered(df$Year)
ggplot(df) +
geom_boxplot(aes(x = Month, y = Value, fill = Type)) +
facet_wrap(~Year)
I'd ideally like to manage this using dplyr and lubridate. Any help would be appreciated!
One option would be to make a true date value, then you can use the date axis formatter. Something like this is a rough start
ggplot(df) +
geom_boxplot(aes(x = lubridate::mdy(paste(Month, 1, Year)), y = Value, fill = Type, group=lubridate::mdy(paste(Month, 1, Year)))) +
scale_x_date(breaks="month", date_labels = "%m")
Do you mean this?
df<-data.frame(Month = rep(c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "February", "March"),each = 20),
Year = rep(c("2018", "2019"), times = c(220, 40)),
Type = rep(c("C", "T"), 260),
Value = runif(260, min = 10, max = 55))
df$Month <- factor(df$Month,levels=c("January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "Dicember"), ordered = T)
df$Month<-ordered(df$Month)
df$Year<-ordered(df$Year)
df$Year_Month <- paste0(df$Month, " ", df$Year)
df$Year_Month <- factor(df$Year_Month, levels = unique(df$Year_Month))
ggplot(df) +
geom_boxplot(aes(x = Year_Month, y = Value, fill = Type))
I am using bootstrap datepicker in my code but the calendar which appears have months like "March","July" etc. Is there any wayto change months full name to "MAR","JUL".
Thanks in advance
See the image
In this I want August to be AUG
Translate to whatever you want:
...simply add a key to $.fn.datepicker.dates,
before calling .datepicker() docs
$.fn.datepicker.dates['en'] = {
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
today: "Today",
clear: "Clear",
format: "mm/dd/yyyy",
titleFormat: "MM yyyy", /* Leverages same syntax as 'format' */
weekStart: 0
};
http://bootstrap-datepicker.readthedocs.io/en/latest/i18n.html
change language:
$('.datepicker').datepicker({
language: "pt-BR"
});
Since you stated in the comments that you are using eonasdan datetimepicker, you can simply use dayViewHeaderFormat option.
Here a working example.
$('#datetimepicker1').datetimepicker({
format: 'L',
dayViewHeaderFormat: 'MMM YYYY'
});
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment-with-locales.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
<div class="input-group date" id="datetimepicker1">
<input type="text" class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
is there any way to show the month number instead of month name in the calender grid?
because Now I can see the calncer as Jan 2014 , Feb 2014 ... etc
is there any way to show the month number like 1 2014 , 2 2014
Thanks
You can redefine this 'months' array before calling datepicker init:
$.fn.datepicker.dates['en'] = {
days: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
daysShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
daysMin: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"],
months: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthsShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
today: "Today",
clear: "Clear"
};
$('#your_datepicker_id').datetimepicker();