Add € sign to data labels in bubble chart - ggplot?
Use package scales
to change the geom_text
label. The only change is
label = dollar(tooandja_kulu_aastas, prefix = "\u20ac")
the rest of the code is exactly the same as in the question.
library(ggplot2)
library(scales)
prx_col_palette <- function(){
c("#E69F00",
"#56B4E9",
"#009E73",
"#F0E442",
"#0072B2",
"#D55E00",
"#CC79A7")
}
prx_cols <- prx_col_palette()
ggplot(palgad_joonisele2,
aes(x = liik,
y = asutus,
colour = liik,
size = tooandja_kulu_aastas)) +
geom_point() +
geom_text(aes(label = dollar(tooandja_kulu_aastas, prefix = "\u20ac")),
colour = "white",
size = 3.5) +
scale_x_discrete(position = "top")+
scale_y_discrete(limits = rev)+
scale_size_continuous(range = c(14, 37)) +
scale_colour_manual(values = prx_cols)+
labs(x = NULL, y = NULL) +
theme(legend.position = "none",
panel.background = element_blank(),
panel.grid = element_blank(),
axis.text.x = element_text(size=11),
axis.text.y = element_text(size=11),
axis.ticks = element_blank())
Created on 2022-03-14 by the reprex package (v2.0.1)
Bubble Chart with ggplot2
As Tom Martens pointed out adjusting alpha can show any overlapping. The following alpha level:
ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
geom_point(aes(size = Quantity, colour = Price, alpha=.02)) +
geom_text(hjust = 1, size = 2) +
scale_size(range = c(1,15)) +
theme_bw()
results in:
Using geom_jitter instead of point, combined with alpha:
ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
geom_jitter(aes(size = Quantity, colour = Price, alpha=.02)) +
geom_text(hjust = 1, size = 2) +
scale_size(range = c(1,15)) +
theme_bw()
produces this:
EDIT: In order to avoid the artefact in the legend the alpha should be placed outside the aes:
ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
geom_point(aes(size = Quantity, colour = Price),alpha=.2) +
geom_text(hjust = 1, size = 2) +
scale_size(range = c(1,15)) +
theme_bw()
resulting in:
and:
ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource, label = Model)) +
geom_jitter(aes(size = Quantity, colour = Price),alpha=.2) +
geom_text(hjust = 1, size = 2) +
scale_size(range = c(1,15)) +
theme_bw()
resulting in:
EDIT 2: So, this took a while to figure out.
I followed the example I linked to in my comment. I adjusted the code to suit your needs. First of all I created the jitter values outside of the plot:
df2$JitCoOr <- jitter(as.numeric(factor(df2$ManufacturingLocation)))
df2$JitCoOrPow <- jitter(as.numeric(factor(df2$PowerSource)))
I then called those values into the geom_point and geom_text x and y coordinates inside aes. This worked by jittering the bubbles and matching labels to them. However it messed up the x and y axis labels so I relabled them as can be seen in scale_x_discrete and scale_y_discrete. Here is the plot code:
ggplot(df2, aes(x = ManufacturingLocation, y = PowerSource)) +
geom_point(data=df2,aes(x=JitCoOr, y=JitCoOrPow,size = Quantity, colour = Price), alpha=.5)+
geom_text(data=df2,aes(x=JitCoOr, y=JitCoOrPow,label=Model)) +
scale_size(range = c(1,50)) +
scale_y_discrete(breaks =1:3 , labels=c("Low","High"," "), limits = c(1, 2))+
scale_x_discrete(breaks =1:4 , labels=c("Location A","Location B","Location C","Location D"), limits = c(1,2,3,4))+
theme_bw()
Which gives this output:
You can adjust the size of the bubbles via scale_size above. I exported this image with dimensions of 1000*800.
Regarding your request to add borders I think it is unnecessary. It is very clear in this plot where the bubbles belong & I think borders would make it look a bit ugly. However, if you still want borders I'll have a look and see what I can do.
Bubble chart with round country flag symbols in ggplot/ggimage
Here's a function that adds a circular mask to each flag. If we start with your plot,
A_plot
We get the urls and create some local filenames:
flags <- A_plot$data$CountryFlag
png_files <- sapply(strsplit(flags, "/"), function(x) x[length(x)])
Now we create some images with a circular mask and save them locally:
OK <- Map(function(flag, png) {
im <- magick::image_read(flag)
im <- magick::image_resize(im, magick::geometry_size_percent(500, 2000))
ii <- magick::image_info(im)
width <- ii$width
fig <- magick::image_draw(magick::image_blank(height, height))
symbols(width/2, width/2, circles=(width/2), bg='black', inches=FALSE, add=TRUE)
im2 <- magick::image_composite(im, fig, operator='copyopacity')
magick::image_write(im2, png)
}, flag = flags, png = png_files)
Now write these file paths as our image locations in the plot object:
A_plot$data$CountryFlag <- png_files
Which changes our plot to:
A_plot
For completeness, we should tidy up after ourselves once the plot is drawn:
sapply(png_files, unlink)
Customised Bubble plot
In order to map a variable in your data to some scale, you use the aes()
function to couple what ggplot2 calls an 'aesthetic' to an expression (typically a symbol for a column in your data). Thus, to make a colour scale, you have to specify a colour
aesthetic inside the aes()
function. In the code below, I also specify an alpha
aesthetic, which is 1 if Value > 0
and 0 otherwise, making the 0-value points completely transparent. I specify I()
to let ggplot2 know that it should take this value literally instead of mapping it to a scale.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.3
Year<-rep(2001:2005, each = 5)
name<-c("John","Ellen","Mark","Randy","Luisa")
Name<-c(rep(name,5))
Value<-sample(seq(0,25,by=1),25)
mydata<-data.frame(Year,Name,Value)
g <- ggplot(mydata, aes(x=Year, y=Name, size = Value)) +
geom_point(aes(colour = Value,
alpha = I(as.numeric(Value > 0))))
Once we have specified the aesthetics, we can begin customising the scales. The typical pattern is scale_{the aesthetic}_{type of scale}
, so we need to add scale_colour_viridis_c()
if we want to map the colour values to the viridis scale (the *_c
is for continuous scales). In the scales, we can specify for example the limits, which you've indicated should be between 1 and 25. Also, I added a scale_size_area()
where we say that we do not want a legend for the size of the points by setting `guide = "none".
g + scale_colour_viridis_c(option = "C", direction = -1,
limits = c(1, 25)) +
scale_size_area(guide = "none") +
theme(axis.line = element_blank(),
axis.text.x=element_text(size=11,margin=margin(b=10),colour="black"),
axis.text.y=element_text(size=13,margin=margin(l=10),colour="black",
face="italic"),
axis.ticks = element_blank(),
axis.title=element_text(size=18,face="bold"),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.text = element_text(size=14),
legend.title = element_text(size=18))
Created on 2021-02-24 by the reprex package (v1.0.0)
Related Topics
Mutate Multiple/Consecutive Columns (With Dplyr or Base R)
R Shiny Loop to Display Multiple Plots
Extent of Boundary of Text in R Plot
How to Color Entire Background in Ggplot2 When Using Coord_Fixed
How to Preserve Continuous (1,2,3,...N) Ranking Notation When Ranking in R
How to Print Double Quotes (") in R
Shift a Column of Lists in Data.Table by Group
Fixing a Multiple Warning "Unknown Column"
Adding Multiple Lag Variables Using Dplyr and for Loops
Insert Missing Time Rows into a Dataframe
Convert List to Named List in R
Update a Ggplot Using a for Loop (R)
What Does < Stand for in Data.Table Joins with On=
Get Names of Column with Max Value for Each Row
Data.Table := Assignments When Variable Has Same Name as a Column