This question already has answers here:
Rotating x label text in ggplot
(1 answer)
How to rotate the axis labels in ggplot2?
(2 answers)
Closed 2 years ago.
When I create a plot and change the axes labels to something other than the default (here, e.g., I am displaying no. of observations for each factor level)
# setup
set.seed(123)
library(ggplot2)
# plot
(p <-
ggplot(mtcars, aes(as.factor(am), wt)) + geom_point() +
scale_x_discrete(labels = c("0\n(n = 19)", "1\n(n = 13)")))
and then rotate the labels, the axes labels revert to the defaults:
# modify the axes label orientation
p + scale_x_discrete(guide = guide_axis(angle = 90))
#> Scale for 'x' is already present. Adding another scale for 'x', which will
#> replace the existing scale.
Is there any way I can both rotate the labels and also preserve the custom text I had entered into those labels?
P.S. And, no, this is not a duplicate of Rotating x label text in ggplot, since I am not struggling to rotate the labels (my question already includes how to do this), but to rotate labels while preserving label text. I think that's a separate issue.
Try this:
# setup
set.seed(123)
library(ggplot2)
# plot
(p <-
ggplot(mtcars, aes(as.factor(am), wt)) + geom_point() +
scale_x_discrete(labels = c("0\n(n = 19)", "1\n(n = 13)"))+
theme(axis.text.x = element_text(angle=90)))
Related
This question already has answers here:
Spacing of discrete axis by a categorical variable
(1 answer)
Expand Categorical x-axis in ggplot
(2 answers)
Closed 3 years ago.
I am creating a plot where i have a discrete y-axis and a continuous x-axis. I want to create the impression of grouping by moving some y-axis ticks closer together and increase the space between the groups. I tried to demonstrate it by whipping something up in paint.
ggplot(data = mpg, aes(y = trans, x = displ, group = 1)) + geom_step()
So what I'm trying to do is move the manual(mx), the auto(sx), the auto(lx) closer together (blue arrows) and increase the space between these groups (red arrows)
My idea was to create empty ticks between the groups, but ggplot is ignoring those:
brks <- mpg$trans %>% unique() %>% sort()
brks <- append(brks, "test", 2)
brks <- append(brks, "", 5)
ggplot(data = mpg, aes(y = trans, x = displ, group = 1)) + geom_step() +
scale_y_discrete(breaks = brks)
Does anybody have an idea how to achieve this? Thanks!
This question already has answers here:
How can I remove the legend title in ggplot2?
(6 answers)
Closed 5 years ago.
I have seen a lot of questions regarding how to remove some elements of the legend (with guides(... = FALSE) for instance, or how to remove the titles in the legend (with theme(legend.title = element_blank())) but I can't find how to remove the title of only one element in the legend.
MWE :
df = data.frame(x = 1:5, y = 2:6, col = c(1,1,1,2,2), alpha = c(1,1,2,2,3))
ggplot(df, aes(x,y)) + geom_point(aes(fill=col, alpha=alpha))
I would like for instance just to remove the alpha title.
EDIT : I know that it is possible to tweak the things manually afterwards by making something like:
p <- ggplot(df, aes(x,y)) + geom_point(aes(fill=col, alpha=alpha))
p$labels$alpha = NULL
p
but I'd like to have it in regular ggplot2 commands, without creating a variable
You can use labs():
ggplot(df, aes(x,y)) +
geom_point(aes(fill=col, alpha=alpha)) +
labs(alpha="")
This question already has answers here:
Customise x-axis ticks
(1 answer)
Increase number of axis ticks
(6 answers)
Customizing x-axis of graph
(2 answers)
Closed 5 years ago.
I am trying to plot two scatter lines in ggplot. The x axis is integers from 1 to 10. Initially I wrote the following code:
library(ggplot2)
Answer <- c(1:10)
EM = c(0.458,0.517,0.4,0.394,0.15,0.15,0.0,0.2,0.14,0.33)
F1 = c( 0.56,0.63,0.632,0.704,0.502,0.524,0.488,0.64,0.5,0.593)
test_data <- data.frame(EM,F1,Answer)
ggplot(test_data, aes(Answer)) +
geom_line(aes(y = EM, colour = "EM")) +
geom_line(aes(y = F1, colour = "F1"))
This results in the following plot
The x axis is continuous here and printing values like 2.5,7.5. To make it factor ie 1,2,3,4,...,10 I tried putting aes(factor(Answer)) but this results in empty plot. How can I fix this?
Keep your data continuous. If you want to change the scale, do just that:
ggplot(test_data, aes(Answer)) +
geom_line(aes(y = EM, colour = "EM")) +
geom_line(aes(y = F1, colour = "F1")) +
scale_x_continuous(breaks = 1:10)
This question already has an answer here:
How to background geom_vline and geom_hline in ggplot 2 in a bubble chart
(1 answer)
Closed 6 years ago.
Is there a way to add a horizontal line to a boxplot in ggplot2, that doesn't cut through the existing plot, but only the spaces in between?
Thanks for your help...
ggplot adds up each geom one after another, so...
library(ggplot2)
df <- data.frame(x = gl(3,1), y = 1:3)
ggplot(df, aes(x,y)) +
geom_hline(yintercept = 1.5) +
geom_col(width = .5)
... places a horizontal line under the bars produced by geom_col.
This question already has answers here:
Turning off some legends in a ggplot
(2 answers)
Closed 4 years ago.
Is it possible to remove certain items from a legend created with ggplot? I have a plot that is faceted, and point sizes provide another dimension to the plot. Since the plot is faceted I do not need to have certain legend items since it is explained by the facet titles, but the legend is still relevant for the point size.
In the plot below I would like to remove the "AREA" legend items since it is already explained by the faceting, but keep the "TOTAL_VOLUME" legend items that explain the point sizes.
Here is the code used to generate the plot:
library(data.table) # Import libraries
library(ggplot2)
library(scales)
set.seed(1234) # Set Seed
area.list <- LETTERS[seq(1:7)] # 7 Possible areas
date.list <- seq(as.Date("2014/03/01"), by="month", length=13)
# Build a random data set
data <- data.table(AREA = sample(area.list, 80, replace=TRUE),
DATE = sample(date.list, 80, replace=TRUE),
VOLUME = rnorm(n=80, mean=100000,sd=40000),
NON_CONFORMING_VOLUME = rnorm(n=80, mean=30000,sd=5000))
# Summarise data by area and date
data <- data[, list(TOTAL_VOLUME=sum(VOLUME),
TOTAL_NC_VOLUME=sum(NON_CONFORMING_VOLUME)),
by=list(AREA, DATE)]
data$PERCENT_NC <- data$TOTAL_NC_VOLUME / data$TOTAL_VOLUME * 100
p <- ggplot(data = data, aes(x = DATE,
y = PERCENT_NC,
colour = AREA)) +
geom_point(aes(size = TOTAL_VOLUME)) +
geom_line() +
facet_grid(. ~ AREA) +
theme(legend.position="bottom", axis.text.x=element_text(angle=90,hjust=1)) +
ggtitle("Percent Non-Conforming by Area by Month") +
labs(x = "Month", y = "% Non-Conforming") +
scale_size_continuous(labels = comma)
plot(p)
I tried adding show_guide=FALSE to geom_point() but that removes both TOTAL_VOLUME and AREA.
Thank you
You can set the guide for each scale in the following way:
p + guides(size = "legend", colour = "none")