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.
Related
This question already has answers here:
How to set multiple legends / scales for the same aesthetic in ggplot2?
(2 answers)
Manually setting group colors for ggplot2
(1 answer)
Changing line colors with ggplot()
(2 answers)
Closed 10 months ago.
does anyone know how I can plot several line graphs in one graph with different color codes?
ggplot(df, aes(x=variable1)) +
geom_line(aes(y=variable2,color=group1))+
geom_line(aes(y=variable3,color=group1))
I would like to have one color code for the first geom_line and a different one for the second geom_line.
color_group <- c("blue","black","yellow2","orange")
color_flag <- c("green","red","yellow2","cyan")
With
scale_colour_manual(values=color_group)
I can only assign a color code to both of them simultaneously and not separately. Thanks for your help!
You could use the ggnewscale package
library(ggnewscale)
ggplot(df, aes(x = variable1)) +
geom_line(aes(y = variable2, color = group1)) +
scale_colour_manual(values = color_group) +
new_scale_color() +
geom_line(aes(y = variable3, color = group1)) +
scale_colour_manual(values = color_flag)
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:
Showing data values on stacked bar chart in ggplot2
(3 answers)
Closed 5 years ago.
In bar chart values are appearing incorrectly.How to correct it.
ggplot(data=df, aes(x=Demand_Supply,fill=time_slot),position = 'stack') +
geom_bar() + facet_wrap(~`Pickup.point`) +
geom_text(stat='count',aes(label=abs(..count..)))
You can control it using vjust and hjust.
Try following:
ggplot(data=df, aes(x=Demand_Supply,fill=time_slot),position = 'stack')+ geom_bar() + facet_wrap(~Pickup.point)+
geom_text(stat='count',aes(label=abs(..count..)), , vjust = -0.5)
you can try different permutation and combination with hjust and vjust.
This question already has an answer here:
How to show abline of geom_abline in legend
(1 answer)
Closed 7 years ago.
I am having trouble getting ggplot to display legends for colors and line types when they are only defined within an geom_abline.
I've added a very simple example below.
It generates the plot as shown (no legends).
I've also tried adding a scale_color_manual to the plot, but that did not appear to do the trick.
Does anybody know how to make ggplot2 display the legends?
library(ggplot2);
xy_data = data.frame(x=runif(100, min=-0.5, max=0.5),
y=runif(100, min=-0.5, max=0.5));
slope_data = data.frame(slope = c(-1, -0.5, 0.5, 1.0),
model = paste0("m", seq(1,4)),
robust = rep(c("Robust", "Non-robust"), 2))
merged_data = merge(xy_data, slope_data)
slope_plot =
ggplot()+
geom_point(data=xy_data, aes(x=x, y=y))+
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust))
ggsave(plot=slope_plot, file="no_legend_slope_plot.png")
You can try:
slope_plot = ggplot() +
geom_point(data=xy_data, aes(x=x, y=y)) +
geom_abline(data=slope_data, aes(intercept=0, slope=slope, color=model, linetype=robust), show_guide=TRUE)
This question already has answers here:
ggplot legend showing transparency and fill color
(1 answer)
How to set legend alpha with ggplot2
(2 answers)
Closed 9 years ago.
In ggplot2, transparency that is defined in geom_XXX is reflected in the legend. For example:
df <- data.frame(x=runif(10000), z=ifelse(runif(10000) > 0.5, 'a', 'b')); df$y <- runif(10000); df$y[df$z == 'b'] <- cos(df$x[df$z == 'b']*10)
ggplot(df) + geom_point(aes(x, y, color=z), alpha=0.1)
Gives the following result:
Since the points are very transparent, they are hardly seen on the legend. I would like to remove point transparency from the legend, so that the graph looks like this:
How is this possible?
You can use function guides() and override.aes= to set alpha value just for legend entries.
ggplot(df) + geom_point(aes(x, y, color=z), alpha=0.1)+
guides(colour = guide_legend(override.aes = list(alpha=1)))