I'm trying to plot my data in R and trying to manually relabel the x axis keeps creating weird extra space on either side of the plot (see pictures).
Here's my code:
ACL_data_frame <- data.frame(ACLdata, Pre_PHQ_Score, Post_PHQ_Score)
colnames(ACL_data_frame)[c(328:329)] <- c("PHQ.1", "PHQ.2")
ACL_data_frame_long <- reshape(ACL_data_frame, direction="long", varying=328:329, sep=".")
ACL_data_frame_long$Condition <- factor(ACL_data_frame_long$Condition)
ggplot(data = ACL_data_frame_long, aes(x = time, y = PHQ, linetype = Condition)) +
stat_summary(fun=mean, geom="line", size=1.25) +
labs(x="Time", y="Depression (PHQ-9)") +
scale_x_discrete(limits=c("1","2"),
labels=c("Pre", "Post")) +
scale_linetype_discrete(name="Condition",
breaks=c("1", "2"),
labels=c("Control", "Intervention"))
The relevant columns in my data basically look like this (but longer):
time
PHQ
Condition
1
0.6666667
1
1
1.1111111
2
2
0.7777778
2
2
1.3333333
1
Does anyone know how to get rid of the weird space on either side of the plot (see pics)? I tried xlim but that gives an error. Removing the scale_x_discrete also fixes it, but then I can't figure out another way to label the x axis correctly (i.e., there are only 2 timepoints so having labels for Time 1.25, Time 1.5, etc doesn't make sense, they need to just be labeled "Pre" and "Post")
Plot using scale_x_discrete, with correct x axis labels but incorrect margins
Plot without scale_x_discrete, with correct margins but incorrect x axis labels
As your time column is a numeric you get a continuous scale by default. Therefore, to set the breaks and labels use scale_x_continuous(breaks = 1:2, labels = c("Pre", "Post")). Adding scale_x_discrete will switch to a discrete scale which by default adds an expansion of .6 to the lower and the upper end of the scale. In contrast, scale_x_continuous adds an expansion of 5% of the data range to the lower and the upper end of the scale.
library(ggplot2)
ggplot(data = ACL_data_frame_long, aes(x = time, y = PHQ, linetype = Condition)) +
stat_summary(fun = mean, geom = "line", size = 1.25) +
labs(x = "Time", y = "Depression (PHQ-9)") +
scale_x_continuous(breaks = 1:2, labels = c("Pre", "Post")) +
scale_linetype_discrete(
name = "Condition",
breaks = c("1", "2"),
labels = c("Control", "Intervention")
)
I have a time series graph of 49 countries, and I'd like to do three things: (1) prevent the country label name from being cut off, (2) specify so that the coloring is based on the position in the graph rather than alphabetically, and (3) specify which countries I would like to label (49 labels in one graph is too many).
library(ggplot2)
library(directlabels)
library(zoo)
library(RColorBrewer)
library(viridis)
colourCount = length(unique(df$newCol))
getPalette = colorRampPalette(brewer.pal(11, "Paired"))
## Yearly Incorporation Rates
ggplot(df,aes(x=year2, y=total_count_th, group = newCol, color = newCol)) +
geom_line() +
geom_dl(aes(label = newCol),
method= list(dl.trans(x = x + 0.1),
"last.points", cex = 0.8)) +
scale_color_manual(values = getPalette(colourCount)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
legend.position = "none") +
labs(title = "Title",
x = "Year",
y = "Count")
This code works -- there are 49 lines, and each of them is labelled. But it just so happens that all the countries with the highest y-values have the same/similar colors (red/orange). So is there a way to specify the colors dynamically (maybe with scale_color_identity)? And how do I add space just on the right side of the labels? I found the expand = expand_scale, but it added space on both sides (though I did read that in the new version, it should be possible to do so.)
I am also fine defining a list of 49 manually-defined colors rather than using the color ramp.
One way to do it is to limit the x axis by adding something like
coord_cartesian(xlim = c(1,44), expand = TRUE)
In this case, I had 41 years of observations on the axis, so by specifying 44, I added space to the x-axis.
Thank you to #JonSpring for the help and getting me to the right answer!
Hi guys, I have this very messy plot. How can I
rotate the x axis text so that you can actually read it
not include every y value in the y-axis (maybe have the y axis in intervals of 5)
add jitter so that the plot is easier is read
remove the NA values (I tried to, but I guess it did not work)
remove the legend (had to crop it for confidentiality)
here is my code:
data <- ndpdata[which(ndpdata$FC.Fill.Size==20),] #20 fill size
library(tidyr)
my_df_long <- gather(data, group, y, -FC.Batch.Nbr)
data = my_df_long[2075:2550,]
ggplot(data, aes(FC.Batch.Nbr, y, color=FC.Batch.Nbr), na.rm=TRUE) + geom_point()
To rotate the x axis add this to your ggplot:
theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))
If you don't want to include every value on the y axis, you can set breaks:
scale_y_continuous(breaks = c(251,270,290,310,325))
To add jitter points try position = "jitter" inside geom_point():
geom_point(position = "jitter")
To remove NA's you can use it on your data:
data <- data[!is.na(data)]
To remove legend add this to your ggplot:
theme(legend.position = "none")
Using ggplot, you can change the width of a bar of a bar graph by modifying width:
geom_bar(stat="identity",position=position_dodge(),width = .9)
You can uniformly change the distance of the bars using position_dodge():
geom_bar(stat="identity",position=position_dodge(1),width = .9)
How do I customize the distance between bars so they are varied in a ununiform manner?
It's not clear exactly what you mean. I'm assuming you mean you have a discrete x axis variable and you wish to specify custom spacing between each bar. It's possible to use position_jitter to get random spacing, though this also affects bar width and I'm guessing is not what you want.
I would probably handle this by using a numeric x scale and relabelling the axis with my factor levels:
library(ggplot2)
ggplot(data = data.frame(x = 1:10 + rep(c(0.1, -0.1), 5), y = sample(11:20))) +
geom_bar(aes(x, y, fill = factor(x)), color = "black", stat = "identity") +
scale_x_continuous(breaks = 1:10 + rep(c(0.1, -0.1), 5),
labels = LETTERS[1:10]) +
guides(fill = guide_none())
Of course, we can only guess at what you really want since you didn't provide a motivating example.
This question already has answers here:
Flip ordering of legend without altering ordering in plot
(3 answers)
Closed 6 years ago.
Consider the following sample data set:
mydata="theta,rho,Response
0,0.8400000,0.0000000
40,0.8400000,0.4938922
80,0.8400000,0.7581434
120,0.8400000,0.6675656
160,0.8400000,0.2616592
200,0.8400000,-0.2616592
240,0.8400000,-0.6675656
280,0.8400000,-0.7581434
320,0.8400000,-0.4938922
0,0.8577778,0.0000000
40,0.8577778,0.5152213
80,0.8577778,0.7908852
120,0.8577778,0.6963957
160,0.8577778,0.2729566
200,0.8577778,-0.2729566
240,0.8577778,-0.6963957
280,0.8577778,-0.7908852
320,0.8577778,-0.5152213
0,0.8755556,0.0000000
40,0.8755556,0.5367990
80,0.8755556,0.8240077
120,0.8755556,0.7255612
160,0.8755556,0.2843886
200,0.8755556,-0.2843886
240,0.8755556,-0.7255612
280,0.8755556,-0.8240077
320,0.8755556,-0.5367990
0,0.8933333,0.0000000
40,0.8933333,0.5588192
80,0.8933333,0.8578097
120,0.8933333,0.7553246
160,0.8933333,0.2960542
200,0.8933333,-0.2960542
240,0.8933333,-0.7553246
280,0.8933333,-0.8578097
320,0.8933333,-0.5588192
0,0.9111111,0.0000000
40,0.9111111,0.5812822
80,0.9111111,0.8922910
120,0.9111111,0.7856862
160,0.9111111,0.3079544
200,0.9111111,-0.3079544
240,0.9111111,-0.7856862
280,0.9111111,-0.8922910
320,0.9111111,-0.5812822
0,0.9288889,0.0000000
40,0.9288889,0.6041876
80,0.9288889,0.9274519
120,0.9288889,0.8166465
160,0.9288889,0.3200901
200,0.9288889,-0.3200901
240,0.9288889,-0.8166465
280,0.9288889,-0.9274519
320,0.9288889,-0.6041876
0,0.9466667,0.0000000
40,0.9466667,0.6275358
80,0.9466667,0.9632921
120,0.9466667,0.8482046
160,0.9466667,0.3324593
200,0.9466667,-0.3324593
240,0.9466667,-0.8482046
280,0.9466667,-0.9632921
320,0.9466667,-0.6275358
0,0.9644444,0.0000000
40,0.9644444,0.6512897
80,0.9644444,0.9997554
120,0.9644444,0.8803115
160,0.9644444,0.3450427
200,0.9644444,-0.3450427
240,0.9644444,-0.8803115
280,0.9644444,-0.9997554
320,0.9644444,-0.6512897
0,0.9822222,0.0000000
40,0.9822222,0.6751215
80,0.9822222,1.0363380
120,0.9822222,0.9125230
160,0.9822222,0.3576658
200,0.9822222,-0.3576658
240,0.9822222,-0.9125230
280,0.9822222,-1.0363380
320,0.9822222,-0.6751215
0,1.0000000,0.0000000
40,1.0000000,0.6989533
80,1.0000000,1.0729200
120,1.0000000,0.9447346
160,1.0000000,0.3702890
200,1.0000000,-0.3702890
240,1.0000000,-0.9447346
280,1.0000000,-1.0729200
320,1.0000000,-0.6989533"
foobar <- read.csv(text = mydata)
Of course Response is a continuous variable, and it should be plotted with a continuous color scale. However, I'm being asked to use a discrete color scale, thus I need to discretize value. My natural approach would be the same as in the second answer to this question:
easiest way to discretize continuous scales for ggplot2 color scales?
i.e.
library(ggplot2)
ggplot(data = foobar, aes(x = theta, y = rho, fill = cut(Response, breaks = 5))) +
geom_tile() +
coord_polar(theta = "x", start = -pi/9) +
scale_x_continuous(breaks = seq(0, 360, by = 45)) +
scale_y_continuous(limits = c(0, 1)) +
scale_fill_brewer(palette = "RdYlGn", direction = -1, name = "Response")
However, I would like the labels to be plotted in decreasing order, i.e., the same order ggplot2 would use if it were a continuous variable. In my example, this means that the label (0.644, 1.08], corresponding to the red color, should be on top, and the label (-1.08, 0.644], corresponding to the blue color, should be at the bottom of the legend. How can I get that?
You can use the guide_legend argument reverse to reverse the legend.
scale_fill_brewer(palette = "RdYlGn", direction = -1, name = "Response",
guide = guide_legend(reverse = TRUE))