I make a plot of Cumulative distribution functions of seven distributions. However, it does not show all the elements of the legend. The plot looks like this:
The distribution does not show "Distribution" in full. Can anyone tell me how to solve this?
The code looks like this:
ggplot(df, aes(x, colour = Distribution)) + stat_ecdf() + scale_color_discrete(labels = c("N(0, 2)", 'Laplace(0, 1)',
TeX("$0.85N(0, 0.2) + 0.15N(0, 20)$"),
'Gamma(0.05, 0.1)', 'Exp(0.9)', TeX("$F_{3, 8}$"), TeX("$P(mu_i=0) = 0.95, P(mu_i=8) = 0.05$"))) +
xlim(-3, 3) +
xlab("Value") +
ylab("Cumulative Density Function") +
theme(legend.text.align = 0, legend.position="bottom", legend.text=element_text(size=10))
Also, I do not want to change the position of legend. Because the plot can be too narrow if put the legend left or right side.
Related
I would like to know if it is possible to display the frequencies at the top of each counting bar in a ggplot histogram.
This is the code I have got so far:
br <- seq(0, 178, 10)
ggplot(dfAllCounts, aes(x=months)) + geom_histogram(aes(months), bins = 30, fill="#d2aa47", color = '#163B8B', size = .8, alpha = 0.3) +
scale_x_continuous(breaks = br)
I would like to display that number of months on top, thanks
Instead of the geom_histogram wrapper, switch to the underlying stat_bin function, where you can use the built in geom="text", combined with the after_stat(count) to add the label to a histogram.
ggplot(mpg,aes(x=displ)) +
stat_bin(binwidth=1) +
stat_bin(binwidth=1, geom="text", aes(label=after_stat(count)), vjust=0)
Modified from https://stackoverflow.com/a/24199013/10276092
I have generated the following regression plot
by the code
ggplot(data, aes(x=EDUCLVL , y=CUMDOSE)) +
geom_point() +
geom_smooth(method="lm", col="black") +
stat_regline_equation() +
theme_bw()
I need help in 2 aspects.
In my x-axis I've values 5,6,10,11,12,15,16,17,18,20,21,22,23,24,25.I would like to show all the values in the x-axis, but R takes only 5 values randomly with a gap of 5, any option to get all the values somehow(irrespective of clarity or size).
The regression equation comes at the top left corner, I would like to place it at the top right corner, any option available to define the equation at the side of choice?
You haven't posted your data, but here is a solution using mtcars that you can tweak to your needs.:
ggplot(mtcars, aes(x=mpg , y=cyl)) +
geom_point() +
geom_smooth(method="lm", col="black") +
scale_x_continuous(breaks = round(seq(min(mtcars$mpg), max(mtcars$mpg), by = 2),1)) +
stat_regline_equation(label.x = 28, label.y = 9) +
theme_bw()
Here is a another approach. It sets the x axis breaks to the specific unique values in EDUCLVL. The parameters label.x.npc and label.y.npc set the relative position of the equation along each axis, where 0 is for bottom/left and 1 is for top/right. These parameters also accept character values like "top" and "centre".
ggplot(data, aes(x=EDUCLVL , y=CUMDOSE)) +
geom_point() +
geom_smooth(method="lm", col="black") +
scale_x_continuous(breaks = unique(sort(data$EDUCLVL))) +
stat_regline_equation(label.x.npc = 0.8, label.y.npc = 1) +
theme_bw()
I want to remove the internal borders from my ggplot, leaving a coloured border around the outside of each bar only. Here is a test data frame, with a stacked bar plot. Ideally, I will end up with the groups in the stack still being a shade of grey, with a colourful outline per box.
test <- data.frame(iso=rep(letters[1:5],3),
num= sample(1:99, 15, replace=T),
fish=rep(c("pelagic", "reef", "benthic"), each=5),
colour=rep(rainbow(n=5),3))
ggplot(data=test, aes(x=iso, y=num, fill=fish, colour=colour)) +
geom_bar(stat="identity") +
theme_bw() +
scale_colour_identity() + scale_fill_grey(start = 0, end = .9)
You can accomplish this by moving the fill and colour aes() settings into two separate geom_bar() elements: one which takes the sum for each iso value (the outline), and another which splits things up by fish:
ggplot(data=test, aes(x=iso, y=num)) +
geom_bar(stat="summary", fun.y="sum", aes(color=colour)) +
geom_bar(stat="identity", aes(fill=fish)) +
theme_bw() +
scale_colour_identity() +
scale_fill_grey(start = 0, end = .9)
In a previous question, I asked about moving the label position of a barplot outside of the bar if the bar was too small. I was provided this following example:
library(ggplot2)
options(scipen=2)
dataset <- data.frame(Riserva_Riv_Fine_Periodo = 1:10 * 10^6 + 1,
Anno = 1:10)
ggplot(data = dataset,
aes(x = Anno,
y = Riserva_Riv_Fine_Periodo)) +
geom_bar(stat = "identity",
width=0.8,
position="dodge") +
geom_text(aes( y = Riserva_Riv_Fine_Periodo,
label = round(Riserva_Riv_Fine_Periodo, 0),
angle=90,
hjust= ifelse(Riserva_Riv_Fine_Periodo < 3000000, -0.1, 1.2)),
col="red",
size=4,
position = position_dodge(0.9))
And I obtain this graph:
The problem with the example is that the value at which the label is moved must be hard-coded into the plot, and an ifelse statement is used to reposition the label. Is there a way to automatically extract the value to cut?
A slightly better option might be to base the test and the positioning of the labels on the height of the bar relative to the height of the highest bar. That way, the cutoff value and label-shift are scaled to the actual vertical range of the plot. For example:
ydiff = max(dataset$Riserva_Riv_Fine_Periodo)
ggplot(dataset, aes(x = Anno, y = Riserva_Riv_Fine_Periodo)) +
geom_bar(stat = "identity", width=0.8) +
geom_text(aes(label = round(Riserva_Riv_Fine_Periodo, 0), angle=90,
y = ifelse(Riserva_Riv_Fine_Periodo < 0.3*ydiff,
Riserva_Riv_Fine_Periodo + 0.1*ydiff,
Riserva_Riv_Fine_Periodo - 0.1*ydiff)),
col="red", size=4)
You would still need to tweak the fractional cutoff in the test condition (I've used 0.3 in this case), depending on the physical size at which you render the plot. But you could package the code into a function to make the any manual adjustments a bit easier.
It's probably possible to automate this by determining the actual sizes of the various grobs that make up the plot and setting the condition and the positioning based on those sizes, but I'm not sure how to do that.
Just as an editorial comment, a plot with labels inside some bars and above others risks confusing the visual mapping of magnitudes to bar heights. I think it would be better to find a way to shrink, abbreviate, recode, or otherwise tweak the labels so that they contain the information you want to convey while being able to have all the labels inside the bars. Maybe something like this:
library(scales)
ggplot(dataset, aes(x = Anno, y = Riserva_Riv_Fine_Periodo/1000)) +
geom_col(width=0.8, fill="grey30") +
geom_text(aes(label = format(Riserva_Riv_Fine_Periodo/1000, big.mark=",", digits=0),
y = 0.5*Riserva_Riv_Fine_Periodo/1000),
col="white", size=3) +
scale_y_continuous(label=dollar, expand=c(0,1e2)) +
theme_classic() +
labs(y="Riserva (thousands)")
Or maybe go with a line plot instead of bars:
ggplot(dataset, aes(Anno, Riserva_Riv_Fine_Periodo/1e3)) +
geom_line(linetype="11", size=0.3, colour="grey50") +
geom_text(aes(label=format(Riserva_Riv_Fine_Periodo/1e3, big.mark=",", digits=0)),
size=3) +
theme_classic() +
scale_y_continuous(label=dollar, expand=c(0,1e2)) +
expand_limits(y=0) +
labs(y="Riserva (thousands)")
I have been trying to plot a Normal Q-Q plot with a red line across, in R with ggplot2. I have been unable to add a legend (with LaTeX math) to explain the red line
Here is the code for the basic figure:
ggplot(stdres_df, aes(sample=stdres)) +
stat_qq(color="black") +
geom_abline(slope = 1,
intercept = 0, color ="red")
Thanks in advance.
To get a legend, you need to map something to a color aesthetic inside a call to aes(). In this case, there's no grouping variable to map to colour, but you can just map colour to the name you want to use for the red line.
The line will be red by default, because ggplot uses hcl(15, 100, 65) (a light red) as the first color in its default color palette. However, you can set the color to whatever you want using scale_colour_manual, as shown in the example below. For example:
set.seed(2)
df <- data.frame(y = rnorm(200))
ggplot(df, aes(sample = y)) +
stat_qq() +
geom_abline(aes(slope=1, intercept=0, colour="Test"), size=1) +
coord_equal(xlim=range(df$y)) +
labs(colour="") +
scale_colour_manual(values="red")
Something like this?
ggplot() +
stat_qq(aes(sample=1:100), distribution = qt,dparams = list(df=5)) +
geom_abline(aes(linetype = "line"), slope = 1, intercept = 0, color ="red") +
geom_text(aes(3, 0, label = "TEXT HERE"))