Labeling with geom_text and geom_text_repel - r

I am trying to put labels using Geom Text. If i use geom_text, i cant see all values. But if i use geom_text_repel, i see all values with duplication. The purpose is to see label all the unique values without any duplication. Can anyone please tell me how it will be done ?
library(ggrepel)
library(ggplot2)
rr <- ggplot(data = staypoint6, aes(staypoints.Stayplon,
staypoints.staypLat))+
geom_line(alpha = 0.5, size = 2, shape = 16, width = 0.000003, height =
0.000003, colour ="darkgoldenrod4") + coord_fixed (ratio = 2) +
labs(x ="Longitude", y = "Latitude") +
theme(axis.title = element_text(size=14),
panel.background = element_rect(fill = "white",size = 0.5, linetype = "dotted"),
panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour = "black"),
panel.grid.minor = element_line(size = 0.5, linetype = 'dotted',colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
axis.text.y = element_text(size=12),
axis.text.x = element_text(size=12)) + geom_text(aes(label =
Location_ids), colour = "dodgerblue4", size=6)
rr + geom_text(aes(label = paste0("(", staypoint6$arri_time, ",",
staypoint6$lev_time, ")")), check_overlap = FALSE, size=4, hjust=0.15,
vjust=2)
rr + geom_text_repel(aes(label = paste0("(", staypoint6$arri_time, ",",
staypoint6$lev_time, ")")), check_overlap = FALSE, size=4, hjust=0.15, vjust=2)

Related

Legend keys overlapping when merging linetype and color

I would like to merge the linetype and color legends to one but the keys are overlapping and cannot distinguish the colors
PLOT <- Plot %>% ggplot() + aes(x = Curvature, y = Moment, colour = Section, linetype = Section) +
geom_line(size = 1) + scale_color_hue() + theme_classic() +
labs(x = "Curvature (1/in)", y = "Moment (kip-ft)", color = element_blank()) +
theme(axis.text.y = element_blank(), axis.text.x = element_blank(),
axis.title.y = element_text(size=20, colour="black", angle = 90, vjust = -3),
axis.title.x = element_text(size=20, colour="black", vjust = 4),
legend.position = c(0.78,0.2), axis.line.y = element_blank(),
axis.line.x = element_blank(), axis.ticks.y = element_blank(),
axis.ticks.x = element_blank(), legend.text = element_text(size=20),
legend.key.height = unit(1.0, 'cm'),legend.key.width=unit(3,"line"),
legend.title = element_blank()) +
scale_colour_manual(values = c("Bare Girder" = "black", "9' Spacing" = "dodgerblue",
"12' Spacing" = "hotpink", "15' Spacing" = "limegreen"), name="x") +
scale_linetype_manual(values = c("Bare Girder" = "dashed", "9' Spacing" = "solid",
"12' Spacing" = "solid", "15' Spacing" = "solid"), name="x") +
geom_segment(aes(x=0,y=0,xend=0,yend=80000), size = 0.7, colour = "black") +
geom_segment(aes(x=0,y=80000,xend=0.00028,yend=80000), size = 0.7, colour = "black") +
geom_segment(aes(x=0,y=0,xend=0.00028,yend=0), size = 0.7, colour = "black") +
geom_segment(aes(x=0.00028,y=0,xend=0.00028,yend=80000), size = 0.7, colour = "black")
The resulting plot is:
The issue is that you are overriding the color in the legend via your geom_segments when settinng color="black". To prevent this you could add inherit.aes=FALSE to each of your geom_segements or set the aesthetics inside geom_line.
BTW: You have two scale_color_s in your code so I dropped the scale_color_hue
Using mtcars as example data:
library(ggplot2)
ggplot(mtcars) +
geom_line(aes(hp, mpg, color = factor(cyl), linetype = factor(cyl))) +
scale_color_manual(values = c("black", "dodgerblue", "hotpink")) +
geom_segment(aes(x=0,y=0,xend=0,yend=80000), size = 0.7, colour = "black") +
geom_segment(aes(x=0,y=80000,xend=0.00028,yend=80000), size = 0.7, colour = "black") +
geom_segment(aes(x=0,y=0,xend=0.00028,yend=0), size = 0.7, colour = "black") +
geom_segment(aes(x=0.00028,y=0,xend=0.00028,yend=80000), size = 0.7, colour = "black")

R ggplot2 scale_shape_manual not working but scale_colour_manual works

I would like to set the color and the shape of my 2 indicators which has been plotted in in two layes. The scale_color_manual works however the scale_shape_manual is not working. By having or not having this line "scale_shape_manual"; the result is the same and shape "16" (filled circle) is picked up?
comp_graph_1 <- ggplot() +
layer( mapping = aes(x=log(FV), y= msd, colour = "Reference"), #factor(Dataset)
data = ref,
stat = "identity",
geom = "point",
position = "identity")+
layer(mapping = aes(x=log(FV), y= msd, colour = "Target"), # "red" "blue"
data = target, #data = target[Is_Phone == 0],
stat = "identity",
geom = "point",
position = "identity")+
theme(panel.background = element_rect(fill = 'white'),
panel.grid = element_line(colour = "grey90") , panel.ontop = FALSE)+
theme(legend.justification = c(0, 0), legend.position = "bottom",
legend.background = element_rect(), legend.title = element_blank(), legend.key = element_rect(fill = "white"),
legend.text = element_text(size = 9,colour = "#7F7F7F"), panel.border = element_blank(),
axis.line = element_line(color = "#7F7F7F"))+
theme(plot.title = element_text(size = 16, colour = "#7F7F7F"),
axis.title.x = element_text(size = 11, hjust = 1, face = "bold", colour = "#7F7F7F"),
axis.title.y = element_text(size = 11, hjust = 1, face = "bold", colour = "#7F7F7F")) +
ggtitle(paste0(x, " / ", y, " distribution ")) + xlab(paste0("log ", x)) + ylab(y) +
scale_color_manual(values = c("Reference" ="#FFC000","Target" = "#00AEEF")) +
scale_shape_manual(values = c("Reference" =17, "Target" = 4))
I think where you have colour = "Target" you need a shape statement as well
shape = "Target" and shape = "Reference" and it should work.

How to add two (same) legends in ggplot and change legend title and labels?

I would like to add two (same) legends in ggplot and also want to change legend title and labels. I have tried this:
library(ggplot2)
ggplot(ToothGrowth, aes(x = len, color=factor(dose), fill= factor(dose))) +
geom_density(alpha=0.4) +
theme(panel.background = element_rect(fill = "khaki1", colour = "darkorchid3", size = 2, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid', colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid', colour = "white"),
plot.background = element_rect(fill = "bisque2"),
text = element_text(colour="blue4"), axis.title = element_text(size = rel(1.25)), axis.text = element_text(colour="blue4", size = 12),
legend.position=c(.90,.85), legend.background =
element_rect(fill="lightsalmon", colour = "tomato3", size = 1.25),
legend.title = element_text(colour="navy", face="bold"),
legend.text = element_text( colour="midnightblue", face="bold"), strip.background = element_rect(fill="olivedrab1", colour = "darkorchid3", size = 2, linetype = "solid"),
strip.text = element_text(colour="coral4", size=12, angle=0, face="bold")) +
scale_fill_discrete(name = "Dose", labels = c("A", "B", "C")) +
facet_wrap(~supp)
but I got this plot:
I want this plot:
Can somebody help me? Thank you.
As #erocoar and others have suggested, grid.arrange from gridExtra is useful here. Borrowing heavily from from the linked question:
library(gridExtra)
out <- by(data = ToothGrowth, INDICES = ToothGrowth$supp, FUN = function(m) {
m <- droplevels(m)
m <- ggplot(m, aes(x = len, fill= factor(dose)), color=factor(dose)) +
geom_density(alpha=0.4) +
theme(panel.background = element_rect(fill = "khaki1", colour = "darkorchid3",
size = 2, linetype = "solid"),
panel.grid.major = element_line(size = 0.5, linetype = 'solid',
colour = "white"),
panel.grid.minor = element_line(size = 0.25, linetype = 'solid',
colour = "white"),
plot.background = element_rect(fill = "bisque2"),
text = element_text(colour="blue4"),
axis.title = element_text(size = rel(1.25)),
axis.text = element_text(colour="blue4", size = 12),
legend.position=c(.90,.85),
legend.background = element_rect(fill="lightsalmon",
colour = "tomato3", size = 1.25),
legend.title = element_text(colour="navy", face="bold"),
legend.text = element_text( colour="midnightblue", face="bold"),
strip.background = element_rect(fill="olivedrab1",
colour = "darkorchid3", size = 2,
linetype = "solid"),
strip.text = element_text(colour="coral4", size=12, angle=0,
face="bold")) +
scale_fill_discrete(name = "Dose", labels = c("A", "B", "C")) +
xlim(0,35) +
ylim(0,0.2) +
ggtitle(m$supp)
})
do.call(grid.arrange, list(grobs = out, ncol = 2))
Some things to note.
I moved the color argument outside of the aes() call and this removed the extra legend.
I manually set the x and y limits for a consistent look.
I needed to add a title.
To get the plots side by side I had to add a second argument to do.call(). When supplying more than one argument it needs to be in a list.
I hope this helps.

Adjusting legend.title ,legend.text and legend color in ggplot2

I am unable to change the color of the Segmentation legend in the plot. I need two different colors for it as a text in the legend and as well as in the visual plot.
er<- ggmap(sq_map2) +
geom_point(data = sisquoc, size = 3, aes(fill = Segmentation)) +
geom_line(data = sisquoc, size = 3, aes(color =SpeedMeterPerSecond)) +
geom_text(data = sisquoc, aes(label = paste(" ",
as.character(Location_ids), sep="")),
angle = 60, hjust = 0, color = "sienna4",size = 6 )
gg<- er + labs(x ="Longitude", y = "Latitude") +
theme(axis.title = element_text(size=20),
panel.background = element_rect(fill = "white",size = 0.5, linetype =
"dotted"),
panel.grid.major = element_line(size = 0.5, linetype = 'dotted',colour
= "black"),
panel.grid.minor = element_line(size = 0.5, linetype = 'dotted',colour
= "black"),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
axis.text.y = element_text(size=18),
axis.text.x = element_text(size=18))
gg + theme(legend.position="right",
legend.title = element_text(colour="Black", size=18),
legend.text = element_text(colour="black", size = 15),
legend.background = element_rect(fill="grey90",
size=0.5, linetype="solid",
colour ="black")) + scale_color_continuous(name="Speed (m/s)\n")
Something like the following should work.
Just specify the legend title explicitly and add \n at the end of the string, which adds an extra blank row:
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col=Petal.Length))+
geom_point() + scale_color_continuous(name="my scale\n")
Alternatively you could try changing the legend orientation, which
however usually is most compact when the legend is at the bottom.
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col=Petal.Length))+
geom_point() + theme(legend.direction = "horizontal", legend.position = "bottom")

Geom point visualization issue in R

This is question linked to previous question (Adjusting legend.title ,legend.text and legend color in ggplot2). I am having issue to change color of the geom points (Run and Walk Segmentation) in the plot. Can anyone please help me in this ? Is there any other way that i can have more better visualizations for the segmentation ? Thanks
er<- ggmap(sq_map2) +
geom_point(data = sisquoc, size = 8, aes(fill = Segmentation)) +
geom_line(data = sisquoc, size = 3, aes(color =SpeedMeterPerSecond)) +
geom_text(data = sisquoc, aes(label = paste(" ",
as.character(Location_ids),
sep="")),
angle = 60, hjust = 0, color = "sienna4",size = 6 )
gg<- er + labs(x ="Longitude", y = "Latitude") +
theme(axis.title = element_text(size=20),
panel.background = element_rect(fill = "white",size = 0.5, linetype =
"dotted"),
panel.grid.major = element_line(size = 0.5, linetype =
'dotted',colour
= "black"),
panel.grid.minor = element_line(size = 0.5, linetype =
'dotted',colour
= "black"),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
axis.text.y = element_text(size=18),
axis.text.x = element_text(size=18))
gg + theme(legend.position="right",
legend.title = element_text(colour="Black", size=18),
legend.text = element_text(colour="black", size = 15),
legend.background = element_rect(fill="grey90",
size=0.5, linetype="solid",
colour ="black")) +
scale_color_continuous(name="Speed (m/s)\n")
I assume that you want to change the colour of the points in the plot.
try + scale_fill_manual(values = c("Run" = "black","Walk" = "grey"))
For geom_point to make use of aes(fill=...) you have to select shapes that can take fill values in addition to colour values, otherwise geom_point takes aes(colour=...). Fill is the appropriate aes to use here since you are already making use of aes(colour=...) for geom_line.
See possible shapes 21 to 25 that take fill values here
Try:
ggmap(sq_map2) +
geom_point(data = sisquoc, size = 8, aes(fill = Segmentation, shape = Segmentation) +
scale_shape_manual(values=c(21, 24))
You can further define fill values using e.g. scale_fill_manual(values=c("red", "blue"))

Resources