X axis in ggplot2: factor [duplicate] - r

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)

Related

Create Plot with 2 Axes in R [duplicate]

This question already has answers here:
Dual y axis (second axis) use in ggplot2
(1 answer)
How can I add second axis labels in ggplot2?
(2 answers)
ggplot with 2 y axes on each side and different scales
(18 answers)
Closed 3 months ago.
I need to plot MeanDailySteps (range 0 to 30000) and MeanDailySleepHours (0-600)on two different Y axis of the same chart with the column data ID (a unique string of discrete Characters which is common to both the Y axis variables) on the X axis. The data is in a dataframe "Steps_Sleep_Data".
I tried using the ggplot and scale_y_continuous as below, unfortunately this doesn't work. I tried other means using par(new = TRUE) which didn't work. I seem to be wrong somewhere.
Please help.
ggplot(data=Steps_Sleep_Data, aes(x=Id, y=MeanDailySteps, fill=Id,colour="red", label=MeanDailySteps))+
geom_bar(stat='identity')+
ggplot(data=Steps_Sleep_Data, aes(x=Id, y=MeanDailySleepMinutes, fill=Id, colour="blue", label=MeanDailySleepMinutes)) +
geom_bar(stat='identity')+
scale_y_continuous(sec.axis = ~.*500)+
theme(axis.text.y.left = element_text(color = "red"),
axis.text.y.right = element_text(color = "blue"))

preserving axes label text when labels are rotated in `ggplot2` [duplicate]

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)))

RStudio: ggplot default color change [duplicate]

This question already has answers here:
how to change the color in geom_point or lines in ggplot [duplicate]
(2 answers)
ggplot2: How to specify multiple fill colors for points that are connected by lines of different colors
(1 answer)
Closed 2 years ago.
I am trying to create a bubble chart using R studio, my codes are as follow:
library(ggplot2)
bone$gr = bone$Line
ggplot(bone, aes(x = Year, y = bup, size = Patients, col = gr)) + geom_point() +
geom_abline(intercept = -1.433254613, slope = 0.000836604, col = "red") +
geom_abline(intercept = -2.384481355, slope = 0.001309739, col = "blue")
gr is a group/class variable that divides the dataset into two groups, I want to show the two groups with different colors as this picture shows:
Is there any way I can change the color of the two groups?

Custom spacing between ticks on discrete axis [duplicate]

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!

R ggplot remove certain items from legend [duplicate]

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")

Resources