how to add text for the dots in ggplot graph R - r

I'm trying to add the numbers for the dots in my graph. thanks for the help!
percentage.no.work <- cleanData %>% group_by(AREA) %>%
summarise(percentage = mean(ESTIMATED.CITY.UNEMPLOYMENT))
ggplot() +
geom_point(data=percentage.no.work, aes(x=AREA, y=percentage), alpha=0.6, color="purple", size=2) +
geom_smooth(method = "lm") +
theme_minimal() + ggtitle("Percentage Estimated City Unemployment") +
ylab("Percentage")

Using a little made-up data, you can add text labels like this. Note, you also need the aes() in ggplot rather than geom_point and a group = 1 so that you get the geom_smooth rendered.
library(tidyverse)
tribble(
~AREA, ~percentage,
"a", 0.2,
"b", 0.4
) |>
ggplot(aes(AREA, percentage, group = 1), alpha = 0.6, color = "purple", size = 2) +
geom_point() +
geom_text(aes(label = percentage), nudge_x = 0.1) +
geom_smooth(method = "lm") +
theme_minimal() +
ggtitle("Percentage Estimated City Unemployment") +
ylab("Percentage")
Created on 2022-06-04 by the reprex package (v2.0.1)

Related

How change color after certain intervals in ggplot2?

I have following data:
dput(data)
I used following code to generate the plot:
p <- ggplot(data, aes(Zscore, ID))
p + geom_point(aes(colour=pval, size=Hit)) +
scale_color_gradientn(colours=rainbow(4), limits=c(0, 1)) +
geom_vline(xintercept=0, size=0.2, colour="blue", linetype="dotted") +
theme(panel.background=element_rect(fill="gray95", colour="gray95"),
panel.grid.major=element_line(size=0.1,linetype='solid', colour="gray90"),
panel.grid.minor=element_line(size=0.1,linetype='solid', colour="gray90"),
axis.title.y=element_blank()) +
expand_limits(x=c(-2,3)) +
scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3)) +
scale_y_discrete(limits=rev(data$ID))
in the output, I want to change the color (to green) of positive Zscore values. Also to increase the hit to 0,10,20,25,50....
The plot below is maybe not what is wanted.
To have the color change to green when the Z-score is positive means it should be mapped to Zscore, not to pval;
Also, the aesthetic size for lines was deprecated in ggplot2 3.4.0, I am using linewidth instead.
library(ggplot2)
p <- ggplot(data, aes(Zscore, ID))
p + geom_point(aes(colour = Zscore > 0, size = Hit)) +
scale_color_manual(
name = 'Z-score',
labels = c("Negative", "Positive"),
values = c(`FALSE` = 'red', `TRUE` = 'green')
) +
scale_size_continuous(breaks = c(0,10,20,25,50, 75, 100)) +
geom_vline(xintercept=0, linewidth=0.2, colour="blue", linetype="dotted") +
theme(panel.background=element_rect(fill="gray95", colour="gray95"),
panel.grid.major=element_line(linewidth=0.1,linetype='solid', colour="gray90"),
panel.grid.minor=element_line(linewidth=0.1,linetype='solid', colour="gray90"),
axis.title.y=element_blank()) +
expand_limits(x=c(-2,3)) +
# scale_x_continuous(breaks=c(-3,-2,-1,0,1,2,3, 4, 5, 6)) +
scale_x_continuous(name = 'Z-score', breaks = pretty(data$Zscore)) +
scale_y_discrete(limits=rev(data$ID))
Created on 2022-11-18 with reprex v2.0.2

How to change a density curve without changing the legend in ggplot2?

I want to change the color of the density curve but when I change it, the border of the boxes from the legend changes too.
Original plot:
When I change the color of the density curve:
Also, if I add lwd=1.2 from the density curve, the legend changes too.
Does anyone know how to fix it?
(I want to change the line width and the colour of the density curve, but I don't want to change how the legend looks).
This is the code:
val1 <- c(2.1490626,3.7928443,2.2035281,1.5927854,3.1399245,2.3967338,3.7915825,4.6691277,3.0727319,2.9230937,2.6239759,3.7664386,4.0160378,1.2500835,4.7648343,0.0000000,5.6740227,2.7510256,3.0709322,2.7998003,4.0809085,2.5178086,5.9713330,2.7779843,3.6724801,4.2648527,3.6841084,2.5597235,3.8477471,2.6587736,2.2742209,4.5862788,6.1989269,4.1167091,3.1769325,4.2404515,5.3627032,4.1576810,4.3387921,1.4024381,0.0000000,4.3999099,3.4381837,4.8269218,2.6308474,5.3481382,4.9549753,4.5389650,1.3002293,2.8648220,2.4015338,2.0962332,2.6774765,3.0581759,2.5786137,5.0539080,3.8545796,4.3429043,4.2233248,2.0434363,4.5980727)
val2 <- c(3.7691229,3.6478055,0.5435826,1.9665861,3.0802654,1.2248374,1.7311236,2.2492826,2.2365337,1.5726119,2.0147144,2.3550348,1.9527204,3.3689502,1.7847986,3.5901329,1.6833872,3.4240479,1.8372175,0.0000000,2.5701453,3.6551315,4.0327091,3.8781182)
df1 <- data.frame(value = val1)
df2 <- data.frame(value = val2)
data <- bind_rows(lst(df1, df2), .id = 'id')
data %>%
ggplot(aes(value)) +
geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
geom_density(lwd = 1.2,
colour = "red") +
facet_grid(id ~ .) +
scale_x_continuous(breaks=pretty(data$value, n=10)) +
ggtitle("My histogram....") +
guides(fill=guide_legend(title="My legend...")) +
theme(strip.text.x = element_blank(),strip.text.y = element_blank())
Thanks very much in advance!
Regards
Add show.legend = FALSE to geom_density()
library(ggplot2)
library(dplyr)
data %>%
ggplot(aes(value)) +
geom_histogram(aes(y=..density.., fill = id), bins=10, col="black", alpha=0.4) +
geom_density(lwd = 1.2, colour = "red", show.legend = FALSE) +
facet_grid(id ~ .) +
scale_x_continuous(breaks=pretty(data$value, n=10)) +
ggtitle("My histogram....") +
guides(fill = guide_legend(title="My legend...")) +
theme(strip.text.x = element_blank(),strip.text.y = element_blank())
Created on 2021-12-17 by the reprex package (v2.0.1)

Merge legend in ggplot when the geoms are different

I have the following code which yields the figure below:
ggplot(data=data.frame(x=x, y=y, mass=mass)) +
geom_line(mapping = aes(x=x, y=y, linetype='Gompertz predicted mass', col='Gompertz predicted mass')) +
geom_point(mapping = aes(x=x, y=mass, shape='Actual mass',col='Actual mass')) +
theme_bw() +
ylab('Mass') +
xlab('t') +
scale_color_manual(name='',values = c("black",'red')) +
scale_linetype_manual(name='',values = c("solid")) +
scale_shape_manual(name='', values = c(19)) +
scale_x_continuous(breaks=seq(4,26,2)) +
ylim(c(0, 20000)) +
ggtitle('Problem 3: Plot of tumor mass with time')
Notice how the legend is separated. I'd like to merge it for shape and color. When the geoms are the same, the technique of using scale_something_manual works perfectly fine to merge the legends. However, I'm having trouble with it here since I have two different geoms.
The problem is similar to the one described in https://github.com/tidyverse/ggplot2/issues/3648. There is no elegant solution at the moment. Because you haven't included any data, I've presumed that your problem is conceptually similar to the plot below:
library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(shape = "Point", colour = "Point")) +
geom_smooth(aes(linetype = "Line", colour = "Line"),
formula = y ~ x, se = FALSE, method = "loess") +
scale_colour_manual(values = c("red", "black")) +
scale_linetype_manual(values = "solid") +
scale_shape_manual(values = 19)
The way to fix the problem is to get rid of the linetype and shape aesthetics and scales, and instead override aesthetics at the level of the legend.
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(colour = "Point")) +
geom_smooth(aes(colour = "Line"),
formula = y ~ x, se = FALSE, method = "loess") +
scale_colour_manual(
values = c("red", "black"),
guide = guide_legend(override.aes = list(shape = c(NA, 19),
linetype = c(1, NA)))
)
Created on 2021-09-04 by the reprex package (v2.0.1)

Add individual observations on ggplot (geom_line)

I have the following data frame data frame and I am plotting the average (Accuracy) per level. But I want to also the individual data points with shapes (e.g.Accuracy1, Accuracy2, Accuracy3 etc) on the line. Anyone who could help me? Thanks
ggplot(data=Accuracy_means, aes(x=Effort_Level, y=Accuracy,
group=1)) +
geom_errorbar(aes(ymin=Accuracy-se, ymax=Accuracy+se), width=.05, size=1) +
geom_line(size=1)+
geom_hline(yintercept=c(-0.5,0.5), linetype="dashed", colour="black", size=0.5)+
ylim(0,1)+
coord_fixed(ratio = 2.5)+
theme_classic()
It's not clear if you want to change the line type. If so, here is an approach using gather from tidyr.
library(tidyverse)
Accuracy_means %>%
gather(key = accuracy_vars, value = values, -Effort_Level, -Accuracy, -se) %>%
ggplot(aes(x=Effort_Level,
y=values)) +
geom_errorbar(aes(ymin=Accuracy-se, ymax=Accuracy+se), colour = "red", width =0.05, size = 0.5) +
geom_line(aes(linetype = accuracy_vars), size=1) +
geom_line(aes(y = Accuracy), colour = "red")+
coord_fixed(ratio = 2.5)+
theme_classic()

ggplot2: doughnuts, how to conditional color fill with if_else

Following guides like ggplot Donut chart I am trying to draw small gauges, doughnuts with a label in the middle, with the intention to put them later on on a map.
If the value reaches a certain threshold I would like the fill of the doughnut to change to red. Is it possible to achieve with if_else (it would be most natural but it does not work).
library(tidyverse)
df <- tibble(ID=c("A","B"),value=c(0.7,0.5)) %>% gather(key = cat,value = val,-ID)
ggplot(df, aes(x = val, fill = cat)) + scale_fill_manual(aes,values = c("red", "yellow"))+
geom_bar(position="fill") + coord_polar(start = 0, theta="y")
ymax <- max(df$val)
ymin <- min(df$val)
p2 = ggplot(df, aes(fill=cat, y=0, ymax=1, ymin=val, xmax=4, xmin=3)) +
geom_rect(colour="black",stat = "identity") +
scale_fill_manual(values = if_else (val > 0.5, "red", "black")) +
geom_text( aes(x=0, y=0, label= scales::percent (1-val)), position = position_dodge(0.9))+
coord_polar(theta="y") +
xlim(c(0, 4)) +
theme_void() +
theme(legend.position="none") +
scale_y_reverse() + facet_wrap(facets = "ID")
Scale fill manual values= if else.... this part does not work, the error says: Error in if_else(val > 0.5, "red", "black") : object 'val' not found. Is it my error, or some other solution exists?
I also realize my code is not optimal, initially gather waited for more variables to be included in the plot, but I failed to stack one variable on top of the other. Now one variable should be enough to indicate the percentage of completion. I realise my code is redundant for the purpose. Can you help me out?
A solution for the color problem is to first create a variable in the data and then use that to map the color in the plot:
df <- tibble(ID=c("A","B"),value=c(0.7,0.5)) %>% gather(key = cat,value = val,-ID) %>%
mutate(color = if_else(val > 0.5, "red", "black"))
p2 = ggplot(df, aes(fill=color, y=0, ymax=1, ymin=val, xmax=4, xmin=3)) +
geom_rect(colour="black",stat = "identity") +
scale_fill_manual(values = c(`red` = "red", `black` = "black")) +
geom_text( aes(x=0, y=0, label= scales::percent (1-val)), position = position_dodge(0.9))+
coord_polar(theta="y") +
xlim(c(0, 4)) +
theme_void() +
theme(legend.position="none") +
scale_y_reverse() + facet_wrap(facets = "ID")
The result would be:

Resources