error with stat_compare_means and multiple groups - r

I would like to label my boxplots with pvalues.
Here is my code:
ggplot(df_annot,aes(x=Insect,y=index,fill=Fungi))+geom_boxplot(alpha=0.8)+
geom_point(aes(fill=Fungi),size = 3, shape = 21,position = position_jitterdodge(jitter.width = 0.02,jitter.height = 0))+
facet_wrap(~Location,scales="free" )+
stat_compare_means(aes(group="Insect"))+
guides(fill=guide_legend("M. robertii")) +
scale_x_discrete(labels= c("I+","I-","soil alone"))+
ylab(index_name)+
theme(plot.title = element_text(size = 18, face = "bold"))+
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14)) +
theme(legend.text=element_text(size=14),
legend.title=element_text(size=14)) +
theme(strip.text.x = element_text(size = 14))
Here is the error message that I'm getting:
Warning messages:
1: Unknown or uninitialised column: 'p'.
2: Computation failed in stat_compare_means(): argument "x" is missing, with no default
3: Unknown or uninitialised column: 'p'.
4: Computation failed in stat_compare_means(): argument "x" is missing, with no default
I've tried moving around the aes() from the main ggplot call to the boxplot call. I've tried different inherit.aes in the stat_compare_means().
I've tried first subsetting the root section and making them separately , but the same error.
Any help is appreciated.
thanks
here is my data:
> dput(df_annot)
structure(list(Location = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Root", "Rhizospheric Soil"
), class = "factor"), Bean = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Bean", "No bean"), class = "factor"),
Fungi = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L), .Label = c("M+", "M-"), class = "factor"), Insect = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Insect",
"NI"), class = "factor"), index = c(2.90952191983974, 3.19997588762484,
2.96753469534499, 2.93030877512644, 2.72220793003196, 3.09008037591454,
2.63687890737919, 2.73583925812843, 3.06766793411045, 3.26431040286099,
3.03361194852963, 2.9181623054061)), row.names = c("S-B1",
"S-B2", "S-B3", "S-BF-1", "S-BF-2", "S-BF-3", "S-BFi-1", "S-BFi-2",
"S-BFi-3", "S-Bi-1", "S-Bi-2", "S-Bi-3"), class = "data.frame")

A possible and easy fix to your error maybe to use the exact variable name (i.e. remove the double quotes from the variable name) rather that the quoted variable name (i.e. character) in the stat_compare_means (), so the function should look like this:
stat_compare_means(aes(group=Insect))
A working example using ggboxplot() is as follows:
library(ggpubr)
boxplot <- ggboxplot(ToothGrowth, x = "dose", y = "len", add = "jitter",
color = "supp", group="supp", palette = "jco", legend.title="Supplier")
boxplot <- boxplot + stat_compare_means(aes(group=supp), label = "p.signif", method="wilcox.test", hide.ns=T, paired=F)
print(bxp.legend)
There is a warning message for the above example, but I do not know how improve the code to remove the warning message:
`cols` is now required.
Please use `cols = c(p)`

Related

ggplot with error bars differentiated by shape of points

Getting an error message that reads ' Error: All unnamed arguments must be length 1'. I am trying to differentiate my line by the shape of the point and I keep getting the errors above. I want to use all solid lines but differentiated by shape. The journal I want to public my work requires black and white instead of color
The plot I want to modify:
tgc <- structure(
list(
GROUP = structure(
c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L),
.Label = c("LLL", "LRL", "RLR", "RRR"),
class = "factor"
),
condition = structure(
c(1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L, 1L, 1L, 2L, 2L, 3L, 3L),
.Label = c("Midline", "No Midline crossing", "Midline crossing"),
class = "factor"
),
names = structure(
c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L),
.Label = c("102", "104"),
class = "factor"
),
Trial_type = structure(
c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("retention", "transfer"),
class = "factor"
),
Training = structure(
c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("right", "left"),
class = "factor"
),
N = c(8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8),
NormalizedJerk = c(2060.1571177375, 1092.701687475,
2981.812368875, 1508.28547575, 2089.925186675, 1269.6704558875,
1391.97364445, 914.38364425, 3900.4119165625, 2335.7186722875,
4015.516784, 2563.16723475, 1411.2016892375, 847.952527975,
1853.413925525, 1394.847246875, 6830.81906275, 3915.019566375,
2310.5893110125, 1023.1702538875, 1340.4653657625, 765.2752940875,
5617.967587, 1705.629421875
),
sd = c(1507.9737264907, 946.522319997832,
1403.37144167604, 813.034284948276, 1383.95826055979, 915.414811222361,
580.358119711544, 361.141583497209, 3283.59638643298, 1471.60790379469,
1178.5979495842, 806.56121914288, 701.001824354642, 324.415375522303,
2103.42765701483, 1208.14874080532, 4671.80701687463, 2861.85154237875,
2130.92970547315, 813.649686635084, 489.544827982279, 256.250905151245,
5305.91963495324, 878.475841087918
),
se = c(533.14922392636,
334.646175507445, 496.166731466336, 287.451028112041, 489.303135460484,
323.648010306967, 205.187580982353, 127.682831329662, 1160.9266357632,
520.291964010475, 416.697301221774, 285.16245374901, 247.841571812654,
114.6981559765, 743.673980005258, 427.145083652716, 1651.73321101347,
1011.8173161826, 753.397422485958, 287.668605464989, 173.080233780536,
90.5983763588182, 1875.92587715314, 310.588112170911
),
ci = c(1260.69758461414,
791.31246230768, 1173.24788605882, 679.713672219011, 1157.01806049025,
765.305934151606, 485.191530116531, 301.92191947415, 2745.15527724569,
1230.29499600801, 985.332544042271, 674.302053778614, 586.052191261077,
271.218041235005, 1758.50952839896, 1010.03762375384, 3905.72840792368,
2392.56776402689, 1781.50181629779, 680.228160904389, 409.269718268804,
214.231117892992, 4435.85982330679, 734.424182295757
)
),
row.names = c(NA, -24L),
class = "data.frame"
)
tgc <- summarySE(
data10,
measurevar = "NormalizedJerk",
groupvars = c("GROUP", "condition", "names","Trial_type", "Training")
)
pd <- position_dodge2(0.2)
p <-ggplot(
gc,
aes(
names,
NormalizedJerk,
group = interaction(Training, Trial_type),
color = interaction(Training, Trial_type),
linetype = interaction(Training, Trial_type),
shape = Training
)
) +
geom_errorbar(
aes(ymin = NormalizedJerk - se, ymax = NormalizedJerk + se),
width = .3,
position = pd
) +
geom_line(
position = pd,
size = 1
) +
geom_point(
aes(shape = Training),
position = pd,
size = 1
)+
scale_colour_manual(
name = "Experimental group",
values = c("#999999","#999999","#000000","#000000")
labels = c("RRR","LLL","LRL","RLR")
) +
scale_shape_manual(
name = "Experimental group",
values = c("19","18","19","18")
) +
scale_linetype_manual(
name = "Experimental group",
values = c("solid","solid","solid","solid"),
labels = c("RRR","LLL","LRL","RLR")
) +
theme_bw() +
facet_wrap(.~condition) +
theme(
axis.title.y = element_text(vjust= 1.8, size = 14),
axis.title.x = element_text(vjust= -0.5, size = 14),
axis.title = element_text(face = "bold")
) +
xlab("Block of trials") +
ylab("Normalized Jerk")
p +
scale_x_discrete(
breaks=c("102","104"),
labels=c("Pretest","Posttest")
)
I modified your code a little. Thanks to Nic3500 for formatting the code.
You needed to add the same labels to the shape manual override as the others and turn the error bar legend off. I also increased the size of the legend because the default seems too small when showing different line types.
ggplot(tgc,
aes(
names,
NormalizedJerk,
group = interaction(Training, Trial_type),
color = interaction(Training, Trial_type),
linetype = interaction(Training, Trial_type),
shape = Training
)
) +
geom_errorbar(aes(ymin=NormalizedJerk-se, ymax=NormalizedJerk+se),
show.legend=FALSE, # <- here
width=.3, position=pd) +
geom_line(position=pd, size = 1) +
geom_point(position=pd, size= 2) +
scale_colour_manual(name = "Experimental group",
values=c("#999999","#999999","#000000","#000000"),
labels=c("RRR","LLL","LRL","RLR")) +
scale_shape_manual(name = "Experimental group", values=c(19,18,19,18),
labels=c("RRR","LLL","LRL","RLR")) + # <- here
scale_linetype_manual(name = "Experimental group",
values=c("solid","dashed","solid","dashed"),
labels=c("RRR","LLL","LRL","RLR")) +
theme_bw()+
facet_wrap(.~condition) +
theme(axis.title.y = element_text(vjust= 1.8, size = 14),
axis.title.x = element_text(vjust= -0.5, size = 14),
axis.title = element_text(face = "bold"),
legend.key.width = grid::unit(1.25, "cm")) + # <- here
xlab("Block of trials") + ylab("Normalized Jerk")

Convert two ggplots into one

I am facing some problem to have one plot instead of two from separate data frames. I explained the situation a bit below. The data frames look like:
df1 <- structure(list(value = c(9921L, 21583L, 11822L, 1054L, 13832L,
16238L, 13838L, 20801L, 20204L, 13881L, 19935L, 13829L, 14012L,
20654L, 13862L, 21191L, 3777L, 15552L, 13817L, 20428L, 16850L,
21003L, 11072L, 22477L, 12321L, 12856L, 16295L, 11431L, 13469L,
14680L, 10552L, 15272L, 9132L, 9374L, 15123L, 22754L, 10363L,
12160L, 13729L, 11151L, 11451L, 11272L, 14900L, 14688L, 17133L,
7315L, 7268L, 6262L, 72769L, 7650L, 16389L, 13027L, 7134L, 6465L,
6490L, 15183L, 7201L, 14070L, 11210L, 10146L), limit = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1Mbit",
"5Mbit", "10Mbit"), class = "factor")), class = "data.frame", row.names = c(NA,
-60L))
df2 <- structure(list(value = c(37262L, 39881L, 30914L, 32976L, 28657L,
39364L, 39915L, 30115L, 29326L, 36199L, 37976L, 36694L, 33718L,
36945L, 33182L, 35866L, 34188L, 33426L, 32804L, 34986L, 29355L,
30470L, 37420L, 26465L, 28975L, 29144L, 27491L, 30507L, 27146L,
26257L, 31231L, 30521L, 30370L, 31683L, 33774L, 35654L, 34172L,
38554L, 38030L, 33439L, 34817L, 31278L, 33579L, 31175L, 31001L,
29908L, 31658L, 33381L, 28709L, 34794L, 34154L, 30157L, 33362L,
30363L, 31097L, 29116L, 27703L, 31229L, 30196L, 30077L), limit = structure(c(3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("180ms",
"190ms", "200ms"), class = "factor")), class = "data.frame", row.names = c(NA,
-60L))
from the data frames above, I have these plots:
limit_bw <- factor(df1$limit, levels = c("1Mbit", "5Mbit", "10Mbit"))
limit_lt <- factor(df2$limit, levels = c("200ms", "190ms", "180ms"))
(to use them sequentially)
bw_line <- ggplot(df1, aes(x = limit_bw, y = value, group=1)) + geom_quantile(method = "loess")
lt_line <- ggplot(df2, aes(x = limit_lt, y = value, group=1)) + geom_quantile(method = "loess")
(I actually have many data so I used geom_quantile())
And also two plots in a grid using rbind/cbind (which is not I want now):
grid.draw(rbind(ggplotGrob(ggplot(df1, aes(limit_bw,value,group=1)) + geom_quantile(method = "loess") + labs(title = "value vs bw",x="bandwidth",y="value")),
ggplotGrob(ggplot(df2, aes(limit_lt, value, group = 1)) + geom_quantile(method="loess") + labs(title="value vs latency", x="latency", y="value")), size = "last"))
I am seeking your help to merge them together into one plot (putting bw_line and lt_line together in the same graph) showing two x-axes either at the top and bottom or two axes in the bottom mentioning their title. Please note, the value has different range for each of the data set. However I need to show two y-axes for separate ranges for each data frame or may be one y-axis showing all the values (min to max) from the both data frame.
I actually seen one very close solution here from #RichieCotton but could not figure out for my data since I have some factors instead of integer values.
I really appreciate your help. Thank you.
I think it's probably easiest to approach this by combining the data into one data frame first. Here I make combined x-values and map your data to those. Then we map as usual, with the addition of a secondary y axis.
library(tidyverse); library(forcats)
# Create shared x axis and combine data frames
limit_combo <- data.frame(level_num = 1:3,
level = as_factor(c("1Mbit\n200ms",
"5Mbit\n190ms",
"10Mbit\n180ms")))
df1b <- df1 %>%
mutate(level_num = limit %>% as.numeric) %>%
left_join(limit_combo)
df2b <- df2 %>%
mutate(level_num = 4 - (limit %>% as.numeric)) %>%
left_join(limit_combo)
df3 <- bind_rows(df1b, df2b, .id = "plot") %>%
mutate(plot = if_else(plot == "1", "bw", "lt"))
# plot with adjusted y values and second axis for reference
ggplot(df3, aes(x = level,
y = value * if_else(plot == "lt", 0.44, 1),
group=plot, color = plot)) +
geom_quantile(method = "loess") +
scale_y_continuous("value", sec.axis = sec_axis(~./0.44)) +
theme(axis.text.y.left = element_text(color = "#F8766D"),
axis.text.y.right = element_text(color = "#00BFC4"))
Here is a different approach to create a single plot from the two datasets which avoids to combine both datasets into one and deal with the factors of limit. df1, df2, limit_bw, and limit_lt are used as given by the OP.
The plot is refined in three steps.
1. Common x axis, common y scale
library(ggplot2)
ggplot() + aes(y = value) +
geom_quantile(aes(x = as.integer(limit_bw), colour = "bw"), df1, method = "loess") +
geom_quantile(aes(x = as.integer(limit_lt), colour = "lt"), df2, method = "loess") +
scale_x_continuous("limit",
breaks = 1:nlevels(limit_bw),
labels = paste(levels(limit_bw), levels(limit_lt), sep = "\n")) +
scale_colour_discrete(NULL)
2. Separate x axes, common y scale
library(ggplot2)
ggplot() + aes(y = value) +
geom_quantile(aes(x = as.integer(limit_bw), colour = "bw"), df1, method = "loess") +
geom_quantile(aes(x = as.integer(limit_lt), colour = "lt"), df2, method = "loess") +
scale_x_continuous("limit",
breaks = 1:nlevels(limit_bw),
labels = levels(limit_bw),
sec.axis = dup_axis(labels = levels(limit_lt))) +
scale_colour_manual(NULL, values = c(bw = "blue", lt = "red")) +
theme(axis.text.x.bottom = element_text(color = "blue"),
axis.text.x.top = element_text(color = "red"))
3. Separate x axes, separate y axes
Here, the y-values of the second dataset are scaled such that the min and max values of the two datasets will coincide.
# compute scaling factor and offset
library(magrittr) # used to improve readability
bw_rng <- loess(df1$value ~ as.integer(limit_bw)) %>% fitted() %>% range()
lt_rng <- loess(df2$value ~ as.integer(limit_lt)) %>% fitted() %>% range()
scl <- diff(bw_rng) / diff(lt_rng)
ofs <- bw_rng[1] - scl * lt_rng[1]
library(ggplot2)
ggplot() +
geom_quantile(aes(x = as.integer(limit_bw), y = value, colour = "bw"),
df1, method = "loess") +
geom_quantile(aes(x = as.integer(limit_lt), y = scl * value + ofs, colour = "lt"),
df2, method = "loess") +
scale_x_continuous("limit",
breaks = 1:nlevels(limit_bw),
labels = levels(limit_bw),
sec.axis = dup_axis(labels = levels(limit_lt))) +
scale_y_continuous(sec.axis = sec_axis(~ (. - ofs) / scl)) +
scale_colour_manual(NULL, values = c(bw = "blue", lt = "red")) +
theme(axis.text.x.bottom = element_text(color = "blue"),
axis.text.x.top = element_text(color = "red"),
axis.text.y.left = element_text(color = "blue"),
axis.text.y.right = element_text(color = "red"))

creating a factor-based in dendrogram with R and ggplot2

This is not so much a coding as general approach call for help ;-) I prepared a table containing taxonomic information about organisms. But I want to use the "names" of these organisms, so no values or anything where you could compute a distance or clustering with (this is also all the information I have). I just want to use these factors to create a plot that shows the relationship. My data looks like this:
test2<-structure(list(genus = structure(c(4L, 2L, 7L, 8L, 6L, 1L, 3L,
5L, 5L), .Label = c("Aminobacter", "Bradyrhizobium", "Hoeflea",
"Hyphomonas", "Mesorhizobium", "Methylosinus", "Ochrobactrum",
"uncultured"), class = "factor"), family = structure(c(4L, 1L,
2L, 3L, 5L, 6L, 6L, 6L, 6L), .Label = c("Bradyrhizobiaceae",
"Brucellaceae", "Hyphomicrobiaceae", "Hyphomonadaceae", "Methylocystaceae",
"Phyllobacteriaceae"), class = "factor"), order = structure(c(1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Caulobacterales",
"Rhizobiales"), class = "factor"), class = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Alphaproteobacteria", class = "factor"),
phylum = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Proteobacteria", class = "factor")), .Names = c("genus",
"family", "order", "class", "phylum"), class = "data.frame", row.names = c(NA,
9L))
is it necessary to set up artificial values to describe a distance between the levels?
Here is an attempt using data.tree library
First create a string variable in the form:
Proteobacteria/Alphaproteobacteria/Caulobacterales/Hyphomonadaceae/Hyphomonas
library(data.tree)
test2$pathString <- with(test2,
paste(phylum,
class,
order,
family,
genus, sep = "/"))
tree_test2 = as.Node(test2)
plot(tree_test2)
many things can be done after like:
Interactive network:
library(networkD3)
test2_Network <- ToDataFrameNetwork(tree_test2, "name")
simpleNetwork(test2_Network)
or graph styled
library(igraph)
plot(as.igraph(tree_test2, directed = TRUE, direction = "climb"))
check out the vignette
using ggplot2:
library(ggraph)
graph = as.igraph(tree_test2, directed = TRUE, direction = "climb")
ggraph(graph, layout = 'kk') +
geom_node_text(aes(label = name))+
geom_edge_link(arrow = arrow(type = "closed", ends = "first",
length = unit(0.20, "inches"),
angle = 15)) +
geom_node_point() +
theme_graph()+
coord_cartesian(xlim = c(-3,3), expand = TRUE)
or perhaps:
ggraph(graph, layout = 'kk') +
geom_node_text(aes(label = name), repel = T)+
geom_edge_link(angle_calc = 'along',
end_cap = circle(3, 'mm'))+
geom_node_point(size = 5) +
theme_graph()+
coord_cartesian(xlim = c(-3,3), expand = TRUE)

How to generate facetted ggplot graph where each facet has ordered data?

I want to sort my factors (Condition, Parameter and SubjectID) by MeanWeight and plot MeanWeight against SubjectID such that when faceted by Condition and Parameter, MeanWeight appears in descending order.
Here is my solution, which isn't giving me what I want:
dataSummary <- structure(list(SubjectID = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("s001",
"s002", "s003", "s004"), class = "factor"), Condition = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("1", "2", "3"), class = "factor"), Parameter = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("(Intercept)", "PrevCorr1", "PrevFail1"), class = "factor"),
MeanWeight = c(-0.389685536725783, 0.200987679398502, -0.808114314421089,
-0.10196105040707, 0.0274188815763494, 0.359978984195839,
-0.554583879312783, 0.643791202050396, -0.145042221940287,
-0.0144598460145723, -0.225804028997856, -0.928152539784374,
0.134025102103562, -0.267448309989731, -1.19980109795115,
0.0587152632631923, 0.0050656268880826, -0.156537446664213
)), .Names = c("SubjectID", "Condition", "Parameter", "MeanWeight"
), row.names = c(NA, 18L), class = "data.frame")
## Order by three variables
orderWeights <- order(dataSummary$Condition, dataSummary$Parameter, dataSummary$SubjectID, -dataSummary$MeanWeight)
## Set factors to the new order. I expect this to sort for each facet when plotting, but it doesn't seem to work.
conditionOrder <- dataSummary$Condition[orderWeights]
dataSummary$Condition <- factor(dataSummary$Condition, levels=conditionOrder)
paramOrder <- dataSummary$Parameter[orderWeights]
dataSummary$Parameter <- factor(dataSummary$Parameter, levels=paramOrder)
sbjOrder <- dataSummary$SubjectID[orderWeights]
dataSummary$SubjectID <- factor(dataSummary$SubjectID, levels=sbjOrder)
## Plot
ggplot(dataSummary, aes(x=MeanWeight, y=SubjectID)) +
scale_x_continuous(limits=c(-3, 3)) +
geom_vline(yintercept = 0.0, size = 0.1, colour = "#a9a9a9", linetype = "solid") +
geom_segment(aes(yend=SubjectID), xend=0, colour="grey50") +
geom_point(size=2) +
facet_grid(Parameter~Condition, scales="free_y")
I tried a few other approaches, but they didn't work either:
dataSummary <- dataSummary[order(dataSummary$Condition, dataSummary$Parameter, dataSummary$SubjectID, -dataSummary$MeanWeight),]
or this one
dataSummary <- transform(dataSummary, SubjectID=reorder(Condition, Parameter, SubjectID, MeanWeight))
You can order your data and plot it. However, the labels no longer correspond to Subject ID's, but to the reordered subjects. If that is not what you want, you cannot use faceting but have to plot the parts separately and use e.g.grid.arrangeto combind the different plots.
require(plyr)
## Ordered data
datOrder <- ddply(dataSummary, c("Condition", "Parameter"), function(x){
if (nrow(x)<=1) return(x)
x$MeanWeight <- x$MeanWeight[order(x$MeanWeight)]
x
})
## Plot
ggplot(datOrder, aes(x=MeanWeight, y=SubjectID)) +
scale_x_continuous(limits=c(-3, 3)) +
geom_vline(yintercept = 0.0, size = 0.1, colour = "#a9a9a9", linetype = "solid") +
geom_segment(aes(yend=SubjectID), xend=0, colour="grey50") +
geom_point(size=2) +
facet_grid(Parameter~Condition) +
scale_y_discrete(name="Ordered subjects")

move axis labels ggplot

I have produced a fact graph in ggplot2 and the x axis title (bottom) is touching the scale values slightly (it's worsened when I plot to .pdf device). How do I move the axis title down a smidge?
DF<-structure(list(race = structure(c(3L, 1L, 3L, 2L, 3L, 1L, 2L,
2L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 3L, 2L, 1L, 2L, 3L), .Label = c("asian",
"black", "white"), class = "factor"), gender = structure(c(1L,
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L), .Label = c("female", "male"), class = "factor"),
score = c(0.0360497844302483, 0.149771418578119, 0.703017688328021,
1.32540102136392, 0.627084455719946, -0.320051801571444,
0.852281028633536, -0.440056896755573, 0.621765489966213,
0.58981396944136, 1.95257757882381, 0.127301498272644, -0.0906338578670778,
-0.637727808028146, -0.449607617033673, 1.03162398117388,
0.334259623567608, 0.0912327543652576, -0.0789977852804991,
0.511696466039959), time1 = c(75.9849658266583, 38.7148843859919,
54.3512613852158, 37.3210772390582, 83.8061071736856, 14.3853324033061,
79.2285735003004, 31.1324602891428, 22.2294730114138, 26.427263191766,
40.5529893144888, 19.2463281412667, 8.45085646487301, 97.6770352620696,
61.1874163107771, 31.3727683430548, 99.4155144857594, 79.0996849438957,
21.2504885323517, 94.1079332400361)), .Names = c("race",
"gender", "score", "time1"), class = "data.frame", row.names = c(NA,
-20L))
require(ggplot2)
p <- ggplot(DF, aes(score, time1, group=gender))
p + geom_point(aes(shape=19)) + facet_grid(race~gender) + scale_x_continuous('BLAH BLAH') +
scale_y_continuous('Some MOre Of theat Good Blahing')
In my data BLAH BLAH is touching the numbers. I need it to move down. How?
You can adjust the positioning of the x-axis title using:
+ opts(axis.title.x = theme_text(vjust=-0.5))
Play around with the -0.5 "vertical justification" parameter until it suits you/your display device.
This is an easy workaround, based on the answer provided here
Just add a line break; \n, at the start of your axes title; xlab("\nYour_x_Label") (Or at the end if you need to move your y label).
It doesn't offer as much control as Eduardo's suggestion in the comments; theme(axis.title.x = element_text(vjust=-0.5)), or use of margin, but it is much simpler!
I would like to note that this is not my answer but #JWilliman - their answer is in the comments on #Prasad Chalasani answer. I am writing this as the current upvoted answers did not actually work well for me but #JWilliman's solution does:
#Answer
+ theme(axis.title.x = element_text(margin = margin(t = 20))
This is because theme(axis.title.x = element_text(vjust = 0.5)) has been superseded and now moves the title/label a fixed distance regardless of the value you put in.

Resources