ggplot2 Creating a side and bottom legend - r

I'm trying to great a plot with one legend on the right side and one on the bottom. Both outside the map. I have tried a few suggestions but either both legends or neither legend moves.
library(ggthemes)
library(ggmap)
library(ggplot2)
d <- data.frame(McMap)
County_Mayo_Map <- get_map("MAP", zoom=9)
p <- ggmap(County_Mayo_Map)
p <- p + geom_point(data=d, aes(lat, lon)) +
geom_point(stat = "identity") +
geom_point(data = d, aes(color = Name), size = 5) +
scale_shape_manual(values=df$x) +
geom_point(data = d, aes(shape = Town), size = 4) +
labs(caption = ("SPM 8-19-17")) +
labs(title = "Title", subtitle = "Subtitle") +
ylab("Latitude") +
xlab("Longitude") +
coord_fixed(ratio = 1/1) +
theme(legend.background = element_rect(
fill ="lemonchiffon",
colour = "black",
size =1)) +
theme_solarized()

Related

R ggplot(): geom_point() with color-palette "Greens" , how to get black point border?

I plot a scatter plot with ggplot() and use a certain color palette, namely 'Greens'. Basically I am very happy with the plot, but I would like to have a black border around each point. My code for the plot is:
p <- ggplot(data = df.dataCorrelation, aes(x = prod1, y = prod2)) +
geom_point(aes(color = year)) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
scale_color_brewer(palette = 'Greens') + # customized color palette
xlab(product1) +
ylab(product2) +
ggtitle("Correlation Scatter Plot (Pearson)") +
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
and provides the following graphic:
I know that I can draw black borders with the command:
geom_point(aes(color = year, fill = ?), color = "black", pch = 21),
but that doesn't work with my selected color palette because I don't know what to use in fill = ?
Try with this.
Here data for a reproducible example
library(dplyr)
product1 <- "product1"
product2 <- "product2"
df.dataCorrelation <- iris %>% rename(prod1 = Petal.Length,
prod2 = Petal.Width,
year = Species)
Here your code
library(ggplot2)
ggplot(data = df.dataCorrelation, aes(x = prod1, y = prod2)) +
geom_point(aes(fill = year), colour = "black", shape = 21, size = 3) +
geom_smooth(method = "lm", se = FALSE, color = "#007d3c") +
theme_classic() +
theme(legend.position = "none") +
theme(panel.background = element_blank()) +
scale_fill_brewer(palette = 'Greens') + # customized color palette
xlab(product1) +
ylab(product2) +
ggtitle("Correlation Scatter Plot (Pearson)") +
theme(plot.title = element_text(hjust = 0.5, face = "bold"))
NOTE that:
I wrote colour = "black"
I changed scale_colour_brewer to scale_fill_brewer
I wrote fill = year instead of colour = year
(I increased the size of the points only because I couldn't see the final result)

"Thermal Bar" plot in R?

I want to create a plot with a single vertical bar (colored continuously), with a mark on it showing the score for a particular person. Image:
I can generate the colored bar in ggplot, but only as a legend (not the actual plot). For example the legend resulting from the following is fine:
ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) +
geom_point() +
scale_color_gradientn(colors = rainbow(5))
Is there any way to do this? Any help would be really appreciated - I'm completely stuck on this.
ggplot(data.frame(y = 51), aes( y=y)) +
geom_tile(data = data.frame(y = 0:100),
aes(x= 0.5, y = y, fill = y)) +
geom_segment(aes(x=0, xend=1, yend=y)) +
geom_text(aes(label = y, x = 1), hjust = -0.3) +
coord_cartesian(clip = "off", xlim = c(0,1.2)) +
scale_fill_gradientn(colors = rainbow(5)) +
scale_x_continuous(labels = NULL) +
guides(fill = FALSE) +
theme_minimal() +
theme(line = element_blank()) +
labs(x="", y = "")

Change the font size of variable names in ggplot

I am not able to increase the font size of the names of the variables in a graphic realized with ggplot.
I tried to include these codes inside ggplot code, but unsuccessfully :
theme(text = element_text(size=20))
theme(axis.text=element_text(size=20))
theme(axis.title=element_text(size=14))
theme_grey(base_size = 20)
geom_text(size=20)
My code is :
library(ggplot2)
library(reshape2)
dataplot <- read.csv("/Documents/R.csv",header=T,sep=";")
dataPlotMelt <- melt(data = dataplot, id.vars = c("variable"),variable.name = "Method",value.name = "SMD")
varNames <- as.character(dataplot$variable)
dataPlotMelt$variable <- factor(dataPlotMelt$variable,levels = varNames)
ggplot(data=dataPlotMelt,mapping=aes(x=variable,y=SMD,group=Method, color=Method))+
ylab("Standardizedmeandifference(%)")+
xlab("") +
geom_point(aes(shape=Method),size=2) +
geom_hline(yintercept=15,color="black",size=0.1,linetype="dashed") +
geom_hline(yintercept=-15,color="black",size=0.1,linetype="dashed") +
coord_flip() +
theme(axis.text.x=element_blank()) +
scale_y_continuous(breaks=c(-65,-15,15,105)) +
theme_bw() +
theme(legend.text=element_text(size=12)) +
theme(legend.title=element_blank(),legend.key=element_blank()) +
scale_colour_manual(values=c("grey","black"))
I'd like to increase the font size of the names of the variables in the graphic and, besides, increase the text "Standardized mean difference (%)" and remove the vertical line between the yintercept and ybreak on both sides
new graphic
Thank you Richard for giving me the solution.
As you suggested I used theme after theme_bw
I managed to suppress the useless vertical lines as well with the command theme(panel.grid.minor = element_blank())
Here is the new code for ggplot :
ggplot(data = dataPlotMelt, mapping = aes(x = variable, y = SMD,group = Method,
color = Method)) +
ylab("Standardized mean difference (%)") + xlab("") +
geom_point(aes(shape = Method),size=2) +
geom_hline(yintercept = 15, color = "black", size = 0.1, linetype = "dashed") +
geom_hline(yintercept = -15, color = "black", size = 0.1, linetype = "dashed") +
coord_flip() +
theme(axis.text.x = element_blank()) +
scale_y_continuous(breaks=c(-65,-15,0,15,105)) +
theme_bw() + theme(legend.text = element_text(size=13)) +
scale_colour_manual(values= c("grey","black")) +
theme(axis.text.y = element_text(size=12)) +
theme(axis.title.x = element_text(size=13)) +
theme(panel.grid.minor = element_blank()) +
theme(legend.title = element_blank(), legend.key=element_blank())

R Plotly Map, Hover Text reading from 2 data frames

I currently have a map that has plotted sites from 2 different data frames. I have red dots for one set and blue dots for another set. I can get the hover text to read from one of the data frames but how do i get it to read from another when hovering over the other color site?
here is my code so far
....Get the world polygon and extract UK
library(maps)
UK <- map_data("world") %>% filter(region=="UK")
png("JCMap.png")
JCMap <- ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", color = "dark grey",alpha=0.3) +
geom_point( data=sitesgeo, aes(x=long, y=lat), colour = 'blue', alpha = 0.5)+
geom_point( data=SCBenchmarks, aes(x=long, y=lat), size = 2, colour = 'red') +
theme_void() + ylim(50,59) + coord_map()+
theme(legend.position="none")+
ggtitle("Sites")+
theme(
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 16, hjust=0.1, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
)
print(JCMap)
JCMap
.....make it interactive!
library(plotly)
p=SCBenchmarks %>%
mutate( mytext=paste("Site: ", site_name, "\n", "Customers: ", claimant_key, sep="")) %>%
ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_point(data=sitesgeo,aes(x=long, y=lat), colour = 'blue', alpha = 0.5) +
geom_point(aes(x=long, y=lat, text=mytext), colour = 'red', alpha = 1) +
scale_size_continuous(range=c(1,15)) +
scale_color_viridis(option="inferno", trans="log" ) +
scale_alpha_continuous(trans="log") +
theme_void() +
ylim(50,59) +
coord_map() +
theme(legend.position = "none")
p=ggplotly(p, tooltip="text")
p
Any help would be much appreciated
Cheers
I figured this out with a little help from a friend, probably not the most convenient way to do it but it works....see code below
######## plot
SCBenchmarks <- SCBenchmarks %>%
mutate( mytext=paste(site_name, "\n", "Customers: ", customer_key, "\n", "Customers per Person: ", Cust_Per_Person, "\n", sep=""))
Final <- Final %>%
mutate( mytext=paste(site_name, "\n", district_name,"\n", "Customers: ", Customer_Count, "\n", sep=""))
## Make the static plot call this text:
p <- ggplot() +
geom_polygon(data = UK, aes(x=long, y = lat, group = group), fill="grey", alpha=0.3) +
geom_jitter(data=Final,aes(x=long, y=lat, text=mytext), colour = 'blue', alpha = 0.5) +
geom_jitter(data=SCBenchmarks,aes(x=long, y=lat, text=mytext), colour = 'red', alpha = 1) +
scale_size_continuous(range=c(1,15)) +
scale_color_viridis(option="inferno", trans="log" ) +
scale_alpha_continuous(trans="log") +
theme_void() +
ylim(50,59) +
coord_map() +
theme(legend.position = "none")
p=ggplotly(p, tooltip="text")
p

Specify colors, axis lines, and removal of background in ggplot2

Where and how do I specify colors, axis lines, and removal of background in geombar? Ultimately, I want to have one bar to be dark gray and one bar to be light gray. They are currently blue and pink which were defaults. I also want the the x and y to have axis lines, and the figure to have no gray background. I have everything else figured out, using the below code. Thank you for your help.
library(ggplot2)
dodge <- position_dodge(width = 0.9)
limits <- aes(ymax = myData$mean + myData$se,
ymin = myData$mean - myData$se)
p <- ggplot(data = myData, aes(x = names, y = mean, fill = names)) +
p + geom_bar(stat = "identity", position = dodge) +
geom_errorbar(limits, position = dodge, width = 0.9) +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank(),
axis.title.x=element_blank())
limits <- aes(ymax = myData$mean + myData$se,
ymin = myData$mean - myData$se)
p <- ggplot(data = myData, aes(x = factor(site), y = mean,
fill = factor(infectionstatus)))
p + geom_bar(stat = "identity",
position = position_dodge(0.9)) +
geom_errorbar(limits, position = position_dodge(0.9),
width = 0.25) +
labs(x = "Sites", y = "Average Calories in White Muscle Tissue") +
scale_fill_discrete(name = "Infection Status")
You probably wanted something like this:
# Generate data
myData <- data.frame(names = letters[1:2],
mean = 1:2,
SE = 0.1)
# Plot data
library(ggplot2)
ggplot(myData, aes(names, mean)) +
geom_bar(aes(fill = names),
stat = "identity", position = "dodge") +
geom_errorbar(aes(ymin = mean - SE, ymax = mean + SE),
position = position_dodge(width = 0.5), width = 0.5) +
labs(title = "Calorie Amount",
subtitle = "Averaged per Tissue",
x = NULL,
y = "Average Calories in White Muscle Tissue",
fill = "Infection Status") +
scale_fill_manual(values = c("grey40", "grey60")) +
theme_classic() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title.x = element_blank())
I used theme_classic() as it does most of the job when you want clean plot. And specified colors with scale_fill_manual(values = c("grey40", "grey60"))

Resources