What is the command for specifying the line colours in a ggplot using geom_step()?
The default colours are working fine, I just want to alter them to my preferred colours.
I have two groups, and my ggplot code starts:
ggplot(WomenAgeComparison, aes(x=IntegerAge, y=CumAgePercent, colour=DataSource)) +
geom_step()
The two groups plot fine. Everything works fine except for changing the line colours.
I have tried:
scale_color_manual(values=c("mediumorchid2",'blue'))
which gave the error
Scale for 'colour' is already present. Adding another scale for 'colour', which will replace the existing scale.
and didn't override the existing colours.
I also tried
scale_color_continuous(values=c("mediumorchid2",'blue'))
which gave the error
Error in continuous_scale(aesthetics, "gradient", seq_gradient_pal(low, :
unused argument (values = c("mediumorchid2", "blue"))
The example of two colours on this page used two separate geom_step() lines. Surely there is an easier way?
What is the command to use? Is there a scale_color command to use, or should it be specified in the aesthetics bracket? If the latter, how do I do that?
Updated to show full ggplot code:
ggplot(WomenAgeComparison, aes(x=IntegerAge, y=CumAgePercent, colour=DataSource)) +
geom_step() +
scale_color_manual(values=c("mediumorchid2",'blue')) +
xlab("Age (years)") + ylab("Cumulative proportion") +
scale_x_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20), expand = c(0, 0)) +
scale_y_continuous(limits = c(0,1), breaks = seq(0, 1, by = .2), expand = c(0, 0)) +
scale_color_discrete(name = "Data source", labels = c("Synthetic data\nestimates", "Timaru Data\nrandom rounded\ncounts")) +
theme(legend.title = element_text(size = 15),
legend.text = element_text(size = 10),
legend.key.height=unit(1.5, "cm"),
axis.text = element_text(size = 10),
axis.title = element_text(size = 15))
I have the scale_color_discrete only to put in the text I want into the legend title and text. I tried to use the theme element_text options to do it instead, and couldn't get that to work.
Related
Say I have the following data:
require(ggplot2)
set.seed(123)
data <- data.frame(x = sample(1:20, 100, replace = TRUE))
I want to create a dot plot of data$x, so this is what I do:
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL)
Which gives me this:
I would like to get rid of those vertical grid lines, so I add theme(line = element_blank()) to my ggplot statement. The problem is that command also eliminates the tick marks, which I would like to keep. How can I hide the grid lines whilst keeping their respective ticks?
I would also like to know how I can change the grid lines so they'll be drawn every 1:20, not at every 0.5 mark.
I've looked for those answers in ?title() and ?geom_dotplot, tried a couple of things, to no avail. Appreciate the help!
Use panel.grid
theme(panel.grid = element_blank())
If you want the grid lines to be drawn every 1:20, not at every 0.5 mark.
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(colour = "white",size=0.75))
you might find answers here
opts has been changed to theme. but the thing about grid.major.x, grid.minor.y should set on the track
To get rid of the major grid line, for example
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL) + theme(panel.grid.major = element_blank())
How do I control the font-family and size for text elements added to my boxplot:
Following the approach in this question, I have implemented the following code to show the number of observations:
library("ggplot2")
v_min <- -1
v_max <- 3.5
increm <- 0.5
y_limits <- c(v_min, v_max)
increms <- seq(v_min, v_max, increm)
counts <- function(x){
# you can experiment with 'adjust' and 'max-value' to find the perfect position
adjust <- 0.95
return(c(y = adjust * v_max, label = length(x)))
}
ggplot(d1, aes(x = ONRC_Hierarchy, y=lwpMeanRut_inc)) +
geom_boxplot(outlier.alpha = 0.2, outlier.size = 0.5) +
geom_hline(aes(yintercept=0), color="blue", linetype="dotted", size=1)+
stat_summary(fun.data = counts, geom = "text") +
scale_y_continuous(limits = y_limits, breaks = increms) +
xlab("") +
ylab("Rut Increment (mm/year)\n") +
theme_minimal() +
theme(
text = element_text(size = 10, family = "mono"),
axis.text.x=element_text(angle = -35, hjust = 0),
panel.grid.major.y = element_line(color = "lightgray",
size = 0.15,linetype = 2),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank())
This solution works, as shown in the plot below, except that the font is different from the other graph elements. As you can see, I have tried to control this with the theme() statement, but it does not seem to work. Note, I deliberately used a small mono font to show the difference in the number of observations labels and the other graph elements.
Geom/stat fonts are set in the geom/stat layer, not in theme(). In this case, you can add family = "mono" as an argument to your stat_summary().
The size of fonts in geom_text/geom_label etc. is not on the same scale as element_text theme options, which are in points. You can read more about that here and here.
I have a rather specific problem for which I am currently looking for a solution and cannot find one.
I would like to create a legend with ggplot where the caption of the legend is below the legend - I don't know how to explain it better. I'm sure the following pictures will help:
How I would like the legend to be:
How the legend is current:
I draw the plot at the end via cowplot on a background (draw_plot), so there is also the possibility of drawing the legend "artificially" over the diagram (draw_text). However, I would have to manage to underline the text - and with the correct linetype (see diagram below). This would actually be my preferred variant - but I have no clue how this could work.
I provide you with the code for the plot and for the character on the background.
I am curious to see if anyone can find a solution! :)
Thank you and best regards
The whole Diagramm:
The ggplot-code (shortened):
colors <- c("#ff9a00","#ff9a00")
PlotLine <- ggplot(pp, aes(x, y, color = id, linetype = id))+
theme(legend.direction="horizontal"
)+
geom_path(size=1.25) +
scale_x_datetime(date_labels = "%Y", breaks = scales::pretty_breaks(n = 3), expand = expansion(mult = c(0.02, 0.03)))+
scale_color_manual(labels = paste("<span style='color:",
colors,
"'>",
unique(c(Nutzer1, Nutzer2)),
"</span>"),
values = colors)+
scale_linetype_manual(labels = paste("<span style='color:",
colors,
"'>",
unique(c(Nutzer1, Nutzer2)),
"</span>"),
values=c("solid","dotted"))+
xlab("")+
ylab("")+
theme(axis.text.x = element_text(size= 10, colour = "black", margin = margin(t = 10, b = -5)))+
theme(axis.text.y = element_blank(), legend.text = element_markdown(size = 8))+
labs(color=' ',linetype=' ')
cowplot-code (shortend):
Ausgabe <- ggdraw() +
draw_plot(PlotLine, width = 0.6, height = 0.18, x = 0.345, y = 0.094)
Data for the Lineplot:
The variables for "Nutzer1" and "Nutzer2" can be filled with anything. Sadly the referred dataframe "pp" is too long to export and post. But i guess any dummy data should do it ;)
Inside your scale_color_manual() add an argument about the "guide":
scale_color_manual(labels = paste("<span style='color:",
colors,
"'>",
unique(c(Nutzer1, Nutzer2)),
"</span>"),
values = colors,
guide = guide_legend(
direction = "horizontal",
label.position = "top"))
This will specify that your label should be on top of the key rather than to the right as it defaults.
I've been trying to change my axes ticks with scale_x_discrete, continuous and nobody seems to work, i either get an error or they just dont change. Also im looking for a way to "move" both of my axes so my plot could look like the provided example.
library(ggplot2)
library(pROC)
library(dplyr)
#some data
data(aSAH)
# store roc object
roc.ob <- roc(outcome ~ s100b, aSAH, percent = T)
ggroc(roc.ob) +
coord_fixed()+
geom_abline(slope = 1 ,intercept = 100) + # add identity line
theme(
panel.background = element_blank(),
axis.title.x = element_text(size =18, face = 'bold'),
axis.title.y = element_text(size =18, face = 'bold'),
panel.border = element_rect(size = 2, fill = NA),
axis.text.x = element_text(size = 14, face ='bold'),
axis.text.y = element_text(size = 14, face ='bold')) +
xlab('100% - Specificity') +
ylab('100% - Sensitivity')
I got this:
But i need to change at free will my axes so they are in probabilities (100 -> 1, 50 -> .5 etc), and my X axis is inverted so 100 -> 0, and 0 -> 100. I give an example, i know i can just change my axis title so its technically correct, but i would want to know if i can change them. And if i could just put the ticks in every side of the plot frame that would be great :D. ¿How could i change my axes?
Yes you can definitely change your axis. It may give a warning because the ggroc() function already reversed the x-axis, but that is not a problem.
Most of the adjustments happen in the scales, a couple of points:
You can change the labelling by providing a custom function to the labels argument, if they underlying data was in percentage space instead of probability space
You can add a secondary axis with identity transformation ~ .x to have tickmarks appear in the top and on the right.
You can set expand to c(0,0) to not have any whitespace padding around the axes. I saw your example didn't have it, so I replicated that.
Lastly, in the theme, you can set the axis tick lengths of the secondary axis to a negative unit, to have the ticks point inward, like the example you gave.
I've not included all your theme settings that were not necessary to demonstrate this.
ggroc(roc.ob) +
coord_fixed()+
geom_abline(slope = 1 ,intercept = 100) + # add identity line
scale_x_continuous(trans = "reverse", name = "100% - Specificity",
labels = function(x){format(x/100)},
sec.axis = sec_axis(~ .x, labels = NULL),
expand = c(0,0)) +
scale_y_continuous(name = "100% - Sensitivity",
labels = function(x){format(x/100)},
sec.axis = sec_axis(~ .x, label = NULL),
expand = c(0,0)) +
theme_bw() +
theme(axis.ticks.length = unit(5, "pt"),
axis.ticks.length.x.top = unit(-5, "pt"),
axis.ticks.length.y.right = unit(-5, "pt"),
panel.grid = element_blank())
Say I have the following data:
require(ggplot2)
set.seed(123)
data <- data.frame(x = sample(1:20, 100, replace = TRUE))
I want to create a dot plot of data$x, so this is what I do:
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL)
Which gives me this:
I would like to get rid of those vertical grid lines, so I add theme(line = element_blank()) to my ggplot statement. The problem is that command also eliminates the tick marks, which I would like to keep. How can I hide the grid lines whilst keeping their respective ticks?
I would also like to know how I can change the grid lines so they'll be drawn every 1:20, not at every 0.5 mark.
I've looked for those answers in ?title() and ?geom_dotplot, tried a couple of things, to no avail. Appreciate the help!
Use panel.grid
theme(panel.grid = element_blank())
If you want the grid lines to be drawn every 1:20, not at every 0.5 mark.
theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(colour = "white",size=0.75))
you might find answers here
opts has been changed to theme. but the thing about grid.major.x, grid.minor.y should set on the track
To get rid of the major grid line, for example
ggplot(data, aes(x)) +
geom_dotplot(binwidth = 1) +
scale_x_continuous(breaks = seq(1, 20, 1)) +
scale_y_continuous(breaks = NULL) + theme(panel.grid.major = element_blank())