Adding labels to scatter plot points in ggplot2 - r

I am having trouble adding labels to points on a scatter plot using ggplot. Instead of adding the country name, it is adding the row number. What changes to geom_text do I need to make to fix this?
ggplot(data = World, aes(x = pop_age, y = peace_index_score, label = country)) + geom_point() + labs(title = "Youth Buldge and Instability", x = "Median Age in Country",y = "Overall Peacefulness of Country") + theme_economist() + ylim(0,4) + xlim(15,45) + geom_smooth(method = lm, color = "red") + geom_text(aes(label=country))

Is this working for you:
ggplot() +
geom_point(data = World, aes(x = pop_age, y = peace_index_score)) +
labs(title = "Youth Buldge and Instability", x = "Median Age in Country",y = "Overall Peacefulness of Country") +
theme_economist() +
ylim(0,4) +
xlim(15,45) +
geom_smooth(data = World, aes(x = pop_age, y = peace_index_score), method = lm, color = "red") +
geom_text(data = World, aes(x = pop_age, y = peace_index_score, label = country))

Related

change starting value for geom_bar

I have a plot that includes data from two different scales. So far, I've plotted both variables and adjusted the scale of one variable (ss) so that it is closer to the other variables. This greatly reduced the white space in the middle of the plot.
set.seed = 42
df <- data.frame(
cat = runif(10, 1, 20),
mean = runif(10, 350, 450),
ss = runif(10, 1, 50))
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
However, I don't love the really long bars for sample size, and I'd like to change the limits on the left y axis so that it starts 250 (where ss = 0). Unfortunately, if I replace my current scale_y_continuous parameter with limits (see below), then the bars disappear. How do I do this?
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(limits = c(250, 510), ### NEW Y AXIS LIMITS
sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw()
EDIT: Updated plot with #AllanCameron's suggestion. This is really close, but it has the values of the bars extend below 0 on the secondary axis.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250,
name = "sample size")) +
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510)) ### NEW
Just expand parameter in scale_y_continuous() to c(0,0).
This tells ggplot2 to not add padding to the plot box.
ggplot(data = df) +
geom_bar(aes(x = cat, y = ss + 250),
stat = "identity",
fill = "red") +
geom_point(aes(x = cat, y = mean)) +
geom_smooth(aes(x = cat, y = mean),
method = "loess", se = TRUE) +
scale_y_continuous(sec.axis = sec_axis(trans = ~.-250, name = "sample size"),
expand = c(0,0)) + # New line here!
labs(y = "mean") +
theme_bw() +
coord_cartesian(ylim = c(250, 510))

R ggplot2 add a legend

As described, I want to add a legend to my graph.
nice_plot <- ggplot() +
geom_line(data = plot_dataframe_SD1, mapping = aes(x = XValues, y = YValues_SD1), color = "blue") +
geom_line(data = plot_dataframe_SD2, mapping = aes(x = XValues, y = YValues_SD2), color = "green") +
xlim(1, 2) +
ylim(1, 5) +
xlab("Standarard Deviation") +
ylab("AV") +
scale_fill_identity(name = 'the fill', guide = 'legend',labels = c('m1')) +
scale_colour_manual(name = 'the colour',
values =c('blue'='blue','green'='green'), labels = c('c2','c1'))
This is my code so far, btut it won't output a legend.
I also don't get an error message.
What is wrong with the code or what do I miss out?

Secondary axis in ggplot [duplicate]

I have the following tibble format and i want to create a chart with two y-axis.
sample <- climate <- tibble(
Month = c("1/1/2019","2/1/2019","3/1/2019","4/1/2019","5/1/2019","6/1/2019","7/1/2019","8/1/2019","9/1/2019","10/1/2019","11/1/2019","12/1/2019","1/1/2020","2/1/2020","3/1/2020"),
Reactions = c(52111,37324,212695,152331,24973,10878,7413,8077,13066,50486,8087,12600,31625,25578,20069),
Ratio = c(1371,1866,6445,4914,925,363,218,245,335,1530,352,525,1506,1112,873)
)
Here's what i tried so far.
ggplot() +
geom_bar(mapping = aes(x = sample$Month, y = sample$Reactions), stat = 'identity') +
geom_line(mapping = aes(x = sample$Month , y = sample$Ratio), size = 2, color = "red") +
scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))
Any help will be appreciated
you have to recode Month column as date, and multiply Ratio times 20 (since you devided second axis by 20):
library(lubridate)
sample$Month <- mdy(sample$Month)
ggplot() +
geom_bar(mapping = aes(x = sample$Month, y = sample$Reactions), stat = 'identity') +
geom_line(mapping = aes(x = sample$Month , y = sample$Ratio*20), size = 2, color = "red") +
scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))
you can also improve your code with use of data variable inside ggplot()
ggplot(sample, aes(x = Month)) +
geom_bar(aes(y = Reactions), stat = 'identity') +
geom_line(aes(y = Ratio*20), size = 2, color = "red") +
scale_y_continuous(name = "Reactions per Month", sec.axis = sec_axis(trans = ~./20, name = "Reactions/ post"))
Plot:

Add item to legend by theme options

I have a data frame d like this:
d <- data.frame("name" = c("pippo","pluto","paperino"),
"id" = c(1,2,3),"count" = c(10,20,30),
"pvalue"=c(0.01,0.02,0.05),
geneRatio=c(0.5,0.8,0.2),
type=c("KEGG","Reactome","Reactome"))
and I plot a dotplot using the library ggplot:
ggplot(data = d,aes(geneRatio,name,size=count,colour = pvalue)) +
geom_point()+
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways")+
theme(axis.text.y = element_text(color=d$type))
This is the plot at the moment
I would like to add to legend the information of "type" contained in dataframe d.
I would like to have a new item in the legend with color red = Reactome and color black= KEGG
Not saying that this is a good idea, but you can add a nonsensical geom to force the adding of a guide:
d <- data.frame("name" = c("pippo","pluto","paperino"),
"id" = c(1,2,3),
"count" = c(10,20,30),
"value"=c(0.01,0.02,0.05),
geneRatio=c(0.5,0.8,0.2),
type=c("KEGG","Reactome","Reactome")
)
library(ggplot2)
ggplot(data = d, aes(geneRatio,name,colour = pvalue)) +
geom_point(aes(size=count))+
geom_polygon(aes(geneRatio,name,fill = type)) +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways") +
scale_fill_manual(values = c('Reactome'='red', 'KEGG'='black')) +
theme(axis.text.y = element_text(color=d$type))
geom_polygon may not work with your actual data, and you may not find a suitable 'nonsensical' geom. I agree with #zx8754, a facet would be clearer:
ggplot(data = d, aes(geneRatio,name,colour = pvalue)) +
geom_point(aes(size=count)) +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways") +
facet_grid(type ~ ., scales = 'free_y', switch = 'y')
You could accomplish this using annotate, but it is a bit manual.
ggplot(data = d, aes(geneRatio, name, size = count, colour = pvalue)) +
geom_point() +
ggtitle("Significantly Pathways") +
xlab("Gene Ratio") +
ylab("Pathways")+
theme(axis.text.y = element_text(color=d$type)) +
annotate("text", x = 0.25, y = 3.5, label = "Reactome", color = "red") +
annotate("text", x = 0.25, y = 3.4, label = "KEGG", color = "black")

Adding Quadrants to R Scatterplots, and lines pointing from plots to their respective labels

With ggplot2, can I add four quadrants. Basically just a line down the middle for the y and x axis (a plus sign)? I also wanted to see if i can have a lines pointing from the labels to their respective dots. The labeling overlap is getting problematic.
data$Goal <- factor(data$Goal, levels = c("KPI",
"Strategic Support and Relationship Management",
"Sales and Marketing Support",
"Google AW Account Management and Product Support"))
library(ggplot2)
ggplot(data = data, aes(x = X, y = Y, color = Goal)) +
geom_point(size=3) +
scale_colour_manual(values = c("blue", "red4", "purple4", "olivedrab")) +
geom_text(aes(label = Label), color = "black",
position = "jitter", hjust=0.6, vjust=1.1, size = 2.5) +
labs(title = "PSAT", x = "Impact on Overall Satisfaction", y = "Higher Rating")
Just doing the same thing (the quadrants part) and have used:
p<-ggplot(survey.df, aes(x=specificity, y=success)) +
geom_point() +
lims(x=c(1,10),y=c(1,10)) +
theme_minimal() +
coord_fixed() +
geom_vline(xintercept = 5) + geom_hline(yintercept = 5)
p
Giving
for the lines pointing from the labels to their respective dots:
ggplot(data = data, aes(x = X, y = Y, color = Goal)) +
geom_point(size=3) +
scale_colour_manual(values = c("blue", "red4", "purple4", "olivedrab")) +
geom_text_repel(aes(label = Label), color = "black", size = 2.5) +
labs(title = "PSAT", x = "Impact on Overall Satisfaction", y = "Higher Rating")

Resources