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"))
Related
This question already has answers here:
Remove space between plotted data and the axes
(3 answers)
Closed 1 year ago.
I am trying to move my y-axis tick marks closer to my heatmap.
Here is the code for the heatmap:
ggplot(p_typ_meta, aes(SampleType_Basic, Phylum, fill= Counts)) +geom_tile()+
scale_fill_gradient2(low="blue", mid="white",high="red",midpoint=.15)+theme(axis.title.x = element_text(size=13),axis.title.y = element_text(size=13),axis.text = element_text(size=11),axis.text.y=element_text(margin = margin(0,0)),legend.title.align=0.5, legend.title = element_text(size=13),legend.text = element_text(size=11),plot.title = element_text(hjust=0.5, size=15)) +labs(x="Sample Type", y="Microbial Phyla", title="Microbial Phyla & Sample Type",fill="Relative Abundance")+theme_classic()
I have tried the following:
theme(axis.text.y=element_text(hjust=0.5))
theme(axis.text.y=element_text(margin = margin(0,0)))
theme(axis.ticks.margin=unit(0,'cm'))
theme(axis.text.y = element_text(margin = margin(r = 0)))
scale_y_discrete(expand = c(0, 0))
scale_y_continuous(expand=c(0, 0))
Is there something I am missing in my code? Or is this just how heatmaps work in ggplot2?
I have attached the heatmap I made, and the red arrows show where I want to move the y axis labels. Thanks for the insight!
Using scale_x_discrete(expand = c(0,0)) does the trick. Since you want to reduce the space on the x-axis you need to use this scale and not the one for the y axis.
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)))
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?
This question already has an answer here:
Adding Percentages to a Grouped Barchart Columns in GGplot2
(1 answer)
Closed 5 years ago.
I have a bar plot with my independent variable on the x axis (Education level), and the count of my dependent variable on the y axis (Default on credit card debt).
ggplot(cleancc, aes(x=factor(Education), fill = factor(DefaultOct05))) + geom_bar()
I'd like to keep everything as is but simply show the percentages for each break in the bar. For example, the blue part of the bar 2 is 23.7%.
As I don't have your dataset I cannot try it, but check out this option with stat_bin():
ggplot(cleancc, aes(x=factor(Education), fill = factor(DefaultOct05))) +
geom_bar() +
stat_bin(geom = "text",
aes(label = paste(round((..count..)/sum(..count..)*100), "%")),
vjust = 5)
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)