Geom point visualization issue in R - 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"))

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")

How do I change a dodged bar graph so the bars follow a selected color pattern,not a gradient, and how to keep bars from changing positions?

csv file I'm working with
***I'm relatively new to R and coding in general. I have done a bit of Googling and trial and error to figure out my mistakes/solve my issues but to no avail. Help is greatly appreciated.
For the file above, I'm creating a dodged bar graph to show each quarter from 2017-2020. However, it produces a gradient and I want individual colors. For example, I want all Q1s to be red. All Q2s to be green, etc. I tried using scale_fill_manual() but had no luck.
gradient result
testplot1<-ggplot(test, aes(x=Year, y=Sales, fill=Sales))+
geom_bar(position = "dodge", stat="identity", aes(group=Q))+
labs(x = "Year", y = "Sales",
title="Sales Year-Over-Year",
subtitles = "Test")+
theme_bw()+
theme_minimal()+
theme(axis.ticks = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank(),
plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))+
theme(legend.position = "none")+
geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
size = 2,position = position_dodge2(width = 1),inherit.aes = TRUE)+
scale_y_continuous(labels=dollar_format(prefix="$"), expand = c(0,0), limits = c(-200000,800000))
testplot1
I tried setting the fill to as.factor(Sales), but then it changes the order of the 2020 quarters. Any help is appreciated.
various colors and rearranged 2020 quarters
testplot<-ggplot(test, aes(x=Year, y=Sales, fill=as.factor(Sales)))+
geom_bar(position = "dodge", stat="identity", aes(group=Q))+
labs(x = "Year", y = "Sales",
title="Sales Year-Over-Year",
subtitles = "Test")+
theme_bw()+
theme_minimal()+
theme(axis.ticks = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank(),
plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))+
theme(legend.position = "none")+
geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
size = 2,position = position_dodge2(width = 1),inherit.aes = TRUE)+
scale_y_continuous(labels=dollar_format(prefix="$"), expand = c(0,0), limits = c(-200000,800000))
testplot
It can be tricky when you're trying to color a variable that you know is a categorical variable (the quarter) but that is listed as a number and thus that R interprets as a continuous variable. You can get around that by coding it as a factor like this:
ggplot(test, aes(x=Year, y=Sales, fill=as.factor(Q)))+
geom_bar(position = "dodge", stat="identity") +
labs(x = "Year", y = "Sales",
title="Sales Year-Over-Year",
subtitles = "Test") +
scale_fill_manual(values = c("red", "green", "blue", "gray")) +
theme_bw() +
theme_minimal() +
theme(axis.ticks = element_blank(),
panel.grid.minor.x=element_blank(),
panel.grid.major.x=element_blank(),
plot.title= element_text(face="bold", size=14, color = "black", hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black"))+
theme(legend.position = "none")+
geom_text(aes(label=dollar(Sales)), hjust = 0.5, vjust = -0.8, color = "black",
size = 2, position = position_dodge2(width = 1),
inherit.aes = TRUE) +
scale_y_continuous(labels=dollar_format(prefix="$"),
expand = c(0,0), limits = c(-200000,800000))
The critical bit is the fill = as.factor(Q).

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")

Labeling with geom_text and geom_text_repel

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)

How to change size of points of legend when 2 legends are present

I have a graph where two legends are present. I need to change the size of the points of one of the legends.
I need to change the bullet size of "market type" in the legend. I use the example here but does not work for my graph.
My code is below:
k <- ggplot(subsetdf) + theme_bw() +
geom_point( aes(y=y, x=x, size =Total.Unit.Count, fill = region), shape=21)+
scale_colour_hue(aes(y=y, x=x),l=50) + # Use a slightly darker palette than normal
geom_text_repel (aes(y=y, x=x, label = rownames(subsetdf))) +
geom_smooth(aes(x=x, y=y),method=lm, # Add linear regression lines
se=FALSE) +
labs(y = "title", x = "title",
title = "title",
size = "size", fill = "fill")+
theme(plot.title = element_text (face = 'bold',size = 21,hjust = 0.5),
legend.text = element_text(size = 16),
legend.title = element_text(size = 18),
axis.title.x = element_text(size=20),
axis.title.y = element_text(size=20),
axis.text.x = element_text(size = 18,angle=45, hjust=1),
axis.text.y = element_text(size = 18,hjust = 1),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
scale_size_continuous(range = c(3,8))+
guides(colour = guide_legend(override.aes = list(size=10)))
You used the fill aesthetic guide, not color. So that is the guide to override.
Below is an example with iris dataset, as you code is not reproducible.
library(ggplot2)
ggplot(iris) +
geom_point(aes(Sepal.Length, Petal.Length, size = Sepal.Width, fill = Species), shape = 21) +
guides(fill = guide_legend(override.aes = list(size=8)))

Resources