I want to create a simple barplot of my data frame:
> dput(corr)
structure(list(`sample length` = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("3s", "10s"), class = "factor"),
feature = structure(c(1L, 1L, 5L, 5L, 2L, 5L, 6L, 5L, 5L,
4L, 1L, 1L, 1L, 1L, 1L, 2L, 5L, 5L, 3L, 4L, 1L, 1L, 1L, 1L
), .Label = c("f0", "f1", "f2", "f3", "f2 prime", "f2-f1"
), class = "factor"), measure = c("meanf0 longterm", "meanf0 longterm st",
"f2' Fant", "f2' Carlson", "F1meanERB", "F2meanERB", "f2-f1 ERB",
"f2' Fant", "f2' Carlson", "F3meanERB", "meanf0 3secs", "meanf0 3secs st",
"meanf0 10secs", "meanf0 longterm", "meanf0 longterm st",
"F1meanERB", "f2' Fant", "f2' Carlson", "F2meanERB", "F3meanERB",
"meanf0 longterm", "meanf0 longterm st", "meanf0 3secs",
"meanf0 3s st"), score = c(0.574361009949897, 0.592472685498182,
0.597453479834514, 0.529641256460457, 0.585994252821649,
0.618734735308094, 0.517715270144259, 0.523916918327387,
0.616237363007349, 0.732926257362305, 0.649505366093518,
0.626628120773466, 0.522527636952945, 0.53968850323167, 0.548664887822775,
0.648294358978928, 0.650806695307235, 0.696797693503567,
0.621298393945597, 0.57140950987443, 0.606634531002859, 0.597064217305556,
0.582534743353082, 0.572808145210493), dimension = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L), .Label = c("1", "2", "3",
"4"), class = "factor")), row.names = c(NA, -24L), class = c("tbl_df",
"tbl", "data.frame"))
I have tried the following code:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single')) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5,color='white')
Giving the following barplot:
However, the labels for 'measure' are being incorrectly assigned to the columns. E.g. for 3s facet plot, under 'dimension 2', the two light blue bars should be labelled as 'f2' Carlson' and 'f2' Fant' but they have been swapped with the other two labels.
I think the levels must be wrong, but I don't understand how!
Any help much appreciated, ta
The problem of switching labels comes from geom_text() not knowing how the information should be split for the purposes of dodging. The solution is to supply a group= aesthetic to geom_text() that matches the fill= aesthetic specified for geom_col().
In the case of geom_col(), you specify aes(fill=feature). The height of the different columns is therefore grouped automatically by corr$feature. You can supply a group= aesthetic as well, but it's unnecessary and the dodging will happen as you expect.
In the case of geom_text(), there is no obvious way to group the data. When you do not specify a group= aesthetic, ggplot2 chooses one of the columns (in this case, the first column number) for grouping. For dodging to work here, you need to specify how the label information is grouped. If you don't have a specific legend-associated aesthetic to choose here, you can use the group= aesthetic to specify group=feature. This let's ggplot2 know that the text labels should be sorted and dodged by grouping according to this column in the data:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single')) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, group=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5,color='white')
As a side note, you don't have to specify the group= aesthetic if you assign a color-based aesthetic (or one that would result in a legend). If we set color=feature with geom_text(), it works without group=. To see the labels, you need to set the alpha for the columns a bit lower, but this should illustrate the point well:
ggplot(data=corr, aes(x=factor(dimension), y=score)) +
geom_col(aes(fill=feature),position=position_dodge2(width=1,preserve='single'), alpha=0.2) +
facet_grid(~`sample length`, scales='free_x',space='free_x') +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)") +
geom_text(aes(label=measure, color=feature),position=position_dodge2(width=0.9, preserve='single'), angle=90,
size=4,hjust=2.5)
Related
I'm trying to fix an issue with my GGBalloonPlot graph with regards to how R processes the axis labels.
By default R plots the data using the labels ranked in reverse alphabetical order but to reveal the pattern of the data, the data need to be plotted in a specific order. The only way I've been able to do trick the software is by manually adding a prefix to each label in my .csv table so that R would rank them properly in my output. This is time consuming since I need to manually order the data first before adding the prefix and then plotting.
I would like to input a character vector (or something like that) which would essentially specify the order in which I want to have the data plotted which would reveal the pattern without the need for a prefix in the label name.
I have made some attempts with "scale_y_discrete" without success. I would also like to do the same thing for the X axis since I've had to use the same "trick" to display the columns in the proper non-alphabetical order which offsets the position of the labels. Any idea on how to get GGplot to display my values as seen in the graph without having to "trick" the software since this is quite time consuming ?
Data + Code
#Assign data to "Stack_Overflow_DummyData"
Stack_Overflow_DummyData <- structure(list(Species = structure(c(8L, 3L, 1L, 5L, 6L, 2L,
7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L, 7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L,
7L, 4L, 8L, 3L, 1L, 5L, 6L, 2L, 7L, 4L), .Label = c("Ani", "Cal",
"Can", "Cau", "Fis", "Ort", "Sem", "Zan"), class = "factor"),
Species_prefix = structure(c(8L, 7L, 6L, 5L, 4L, 3L, 2L,
1L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 8L, 7L, 6L, 5L, 4L, 3L,
2L, 1L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L), .Label = c("ac.Cau",
"ad.Sem", "af.Cal", "ag.Ort", "as.Fis", "at.Ani", "be.Can",
"bf.Zan"), class = "factor"), Dist = structure(c(2L, 3L,
5L, 2L, 1L, 1L, 4L, 5L, 2L, 3L, 5L, 2L, 1L, 1L, 4L, 5L, 2L,
3L, 5L, 2L, 1L, 1L, 4L, 5L, 2L, 3L, 5L, 2L, 1L, 1L, 4L, 5L
), .Label = c("End", "Ind", "Pan", "Per", "Wid"), class = "factor"),
Region = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Cen", "Col",
"Far", "Nor"), class = "factor"), Region_prefix = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L), .Label = c("a.Far", "b.Nor", "c.Cen", "d.Col"), class = "factor"),
Frequency = c(75, 50, 25, 50, 0, 0, 0, 0, 11.1, 22.2, 55.6,
55.6, 11.1, 0, 5.6, 0, 0, 2.7, 36.9, 27.9, 65.8, 54.1, 37.8,
28.8, 0, 0, 0, 3.1, 34.4, 21.9, 78.1, 81.3)), class = "data.frame", row.names = c(NA,
-32L))
# Plot Data With Prefix Trick
library(ggplot2)
library(ggpubr)
# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region_prefix", y = "Species_prefix",
size = "Frequency", size.range = c(1, 9), fill = "Dist") +
theme_set(theme_gray() +
theme(legend.key=element_blank())) +
# Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) +
# Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4)
# Add Frequency Values Next to the circles
# Plot Data Without Prefix Trick
library(ggplot2)
library(ggpubr)
# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region", y = "Species",
size = "Frequency", size.range = c(1, 9), fill = "Dist") +
theme_set(theme_gray() +
theme(legend.key=element_blank())) +
# Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) +
# Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4)
# Add Frequency Values Next to the circles
Here below are the graphs
Good Graph.
Using the label prefix trick with the visible pattern in the data:
Wrong Graph (R default).
Without the prefix trick when GGplot automatically orders the data/labels and the graph makes no sense:
To sum up, I would like the Good graph output without having to have to previously add a prefix in my labels.
Many Thanks in advance for your help.
For the axis labels I would define a previous function to override the breaks:
shlab <- function(lbl_brk){
sub("^[a-z]+\\.","",lbl_brk) # removes the starts of strings as a. or ab.
}
Then, to change the labels you just have to use scale_x,y_discrete with labels = shlab (if you look at the help of scale_x_discrete you will see that one of the options for labels is A function that takes the breaks as input and returns labels as output).
For the colours would be enough to change them (values) in scale_fill_manual and for the sizes, using guides so:
library(ggplot2)
library(ggpubr)
shlab <- function(lbl_brk){
sub("^[a-z]+\\.","",lbl_brk)
}
ggballoonplot(Stack_Overflow_DummyData, x = "Region_prefix", y = "Species_prefix", size = "Frequency", size.range = c(1, 9), fill = "Dist") +
scale_x_discrete(labels = shlab) +
scale_y_discrete(labels = shlab) +
scale_fill_manual(values = c("green", "blue", "red", "black", "white")) +
guides(fill = guide_legend(override.aes = list(size=8))) +
theme_set(theme_gray() + theme(legend.key=element_blank())) + # Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) + # Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4) # Add Frequency Values Next to the circles
UPDATE:
With the new dataset and vector labels:
library(ggplot2)
library(ggpubr)
# make color base on Dist, size and alpha dependent on Frequency
ggballoonplot(Stack_Overflow_DummyData, x = "Region", y = "Species",
size = "Frequency", size.range = c(1, 9), fill = "Dist") +
scale_y_discrete(limits = c("Cau", "Sem", "Cal", "Ort", "Fis", "Ani", "Can", "Zan")) +
scale_x_discrete(limits = c("Far", "Nor", "Cen", "Col")) +
theme_set(theme_gray() +
theme(legend.key=element_blank())) +
# Sets Grey Theme and removes grey background from legend panel
theme(axis.title = element_blank()) +
# Removes X axis title (Region)
geom_text(aes(label=Frequency), alpha=1.0, size=3, nudge_x = 0.4)
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")
Hi have this dataset :
tdat=structure(list(Condition = structure(c(1L, 3L, 2L, 1L, 3L, 2L,
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L,
3L, 2L, 1L, 3L, 2L), .Label = c("AS", "Dup", "MCH"), class = "factor"),
variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), .Label = c("Bot", "Top", "All"), class = "factor"),
value = c(1.782726022, 1, 2.267946449, 1.095240234, 1, 1.103630141,
1.392545278, 1, 0.854984833, 4.5163067, 1, 4.649271897, 0.769428018,
1, 0.483117123, 0.363854608, 1, 0.195799358, 0.673186975,
1, 1.661568993, 1.174998373, 1, 1.095026419, 1.278455823,
1, 0.634152231)), .Names = c("Condition", "variable", "value"
), row.names = c(NA, -27L), class = "data.frame")
> head(tdat)
Condition variable value
1 AS Bot 1.782726
2 MCH Bot 1.000000
3 Dup Bot 2.267946
4 AS Bot 1.095240
5 MCH Bot 1.000000
6 Dup Bot 1.103630
I can plot it like that using this code :
ggplot(tdat, aes(x=interaction(Condition,variable,drop=TRUE,sep='-'), y=value,
fill=Condition)) +
geom_point() +
scale_color_discrete(name='interaction levels')+
stat_summary(fun.y='mean', geom='bar',
aes(label=signif(..y..,4),x=as.integer(interaction(Condition,variable))))
I have 2 questions :
How to change the overlay so the black points are not hidden by the
bar chart (3points should be visible per column)
How to add vertical errorbar on top of the bars using the standard
deviation from the black points ?
I'm not much in favor of mixing error bars with a bar plot.
In ggplot2 geoms are drawn in the order you add them to the plot. So, in order to have the points not hidden, add them after the bars.
ggplot(tdat, aes(x=interaction(Condition,variable,drop=TRUE,sep='-'), y=value,
fill=Condition)) +
stat_summary(fun.data="mean_sdl", mult=1, geom="errorbar") +
stat_summary(fun.y='mean', geom='bar') +
geom_point(show_guide=FALSE) +
scale_fill_discrete(name='interaction levels')
Like this:
tdat$x <- with(tdat,interaction(Condition,variable,drop=TRUE,sep='-'))
tdat_err <- ddply(tdat,.(x),
summarise,ymin = mean(value) - sd(value),
ymax = mean(value) + sd(value))
ggplot(tdat, aes(x=x, y=value)) +
stat_summary(fun.y='mean', geom='bar',
aes(label=signif(..y..,4),fill=Condition)) +
geom_point() +
geom_errorbar(data = tdat_err,aes(x = x,ymin = ymin,ymax = ymax,y = NULL),width = 0.5) +
labs(fill = 'Interaction Levels')
I've cleaned up your code somewhat. You will run into fewer problems if you move any extraneous computations outside of your ggplot() call. Better to create the new x variable first. Everything is more readable that way too.
The overlaying issue just requires re-ordering the layers.
Note that you were using scale_colour_* when you had mapped fill not colour (this is a very common error).
The only other "trick" was the un-mapping of y. Normally, when things get tricky I omit aes from the top level ggplot call entirely to make sure that each layer gets only the aesthetics that it needs.
The error bars again I tend to create the data frame outside of ggplot first. I find that cleaner and easier to read.
a <- structure(list(
X1 = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L),
.Label = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8"), class = "factor"),
X2 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L),
.Label = c("A", "B", "C"), class = "factor"),
value = c(0.03508924, 0.03054929, 0.03820896, 0.18207091, 0.25985142, 0.03909991, 0.03079736,
0.41436334, 0.02957787, 0.03113289, 0.03239794, 0.1691519, 0.16368845, 0.0287741, 0.02443448,
0.33474091, 0.03283068, 0.02668754, 0.03597605, 0.17098721, 0.23048966, 0.0385765, 0.02597068, 0.36917749),
se = c(0.003064016, 0.003189752, 0.003301929, 0.006415592, 0.00825635, 0.003479607,
0.003195332, 0.008754099, 0.005594554, 0.006840959, 0.006098068, 0.012790908, 0.014176414,
0.006249045, 0.005659445, 0.018284739, 0.005051873, 0.004719352, 0.005487301, 0.011454206,
0.01290797, 0.005884275, 0.004738851, 0.014075813)),
.Names = c("X1", "X2", "value", "se"), class = "data.frame", row.names = c(NA, -24L))
I'm plotting the above data (kept in dataset "a"), and I can't get the confidence interals to sit in the middle of the group chart.My attempts until now have only managed to put lines on the side of each bar, not in the middle like in the geom_errorbar helpfile.I've tried to manipulate the dodge parameters but it only made it worse.
The chart needs to stay flipped over and in the code below I used geom_linerange but geom_errorbar would be even better.
Another thing I haven't quite managed to do is to change the scale into whole numbers (without muliplying the original table ).
I've used the code below on a<-a[1:16,] (the first two groups).
When I use the same code on the full table I get even worse results with the confidence intervals.
Would anyone be able to help? Many thanks in advance.
limits <- aes(ymax = value + se, ymin=value - se)
p<-ggplot(data = a, aes(x = X1, y =value))+
geom_bar(aes(fill=X2),position = "dodge") +
scale_x_discrete(name="")+
scale_fill_manual(values=c("grey80","black","red"))+
scale_y_continuous(name="%")+
theme(axis.text.y = element_text(face='bold'),
legend.position ="top",
legend.title=element_blank())+
coord_flip()
p + geom_linerange(limits)
Try this ,
p<-ggplot(data = df, aes(x = X1, y =value,fill= X2))+
geom_bar(position=position_dodge()) +
geom_errorbar(aes(ymax = value + 2* se, ymin=value,colour = X2),position=position_dodge(.9))
p <- p + scale_x_discrete(name="")+
scale_fill_manual(values=c("grey80","black","red"))+
scale_y_continuous(name="%")+
theme(axis.text.y = element_text(face='bold'),
legend.position ="top",
legend.title=element_blank())+
coord_flip()
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.