I'm trying to make a simple plot in R using ggplot2. The data is stored in a dataframe with its column written in Russian. The problem is that the contents of the label is shifted from the right border of the latter. These extra spaces appeared whether the label names are defined explicitly (the code below) or implicitly from the dataframe column names.
ggplot(mtcars, aes(x = drat, y =mpg, color = cyl))+
geom_point() +
labs(color = "Русское название") +
theme(legend.background = element_rect(color = "black", linetype = "solid", size = 0.7),
legend.justification = c(1, 1),
legend.position = c(1, 1),
legend.title.align = 0)
The plot with the English title is depicted appropriately.
The encoding of the operational system is set as follows:
"LC_COLLATE=Russian_Russia.1251;LC_CTYPE=Russian_Russia.1251;LC_MONETARY=Russian_Russia.1251;LC_NUMERIC=C;LC_TIME=Russian_Russia.1251"
Is there a way to cope this problem?
I have faced the same issue when needed legend with cyrillic symbols. Looks like it is known issue reported in ggplot2 repo because of the default graphic device on Windows. Actually, if you would try to save your ggplot with ggsave, probably you won't get that issue.
I have not tried to save files by myself yet, but I have followed the reported issue and found some workaround specific for R studio here and it has worked for me, maybe it can solve your issue too. Sample code to run before plotting anything is:
trace(grDevices:::png, quote({
if (missing(type) && missing(antialias)) {
type <- "cairo-png"
antialias <- "subpixel"
}
}), print = FALSE)
Related
So created a loadings plot via arrow style using ggplot command. In order to make things easier for graphing, I added a column into the dataframe of my rr.pr$rotation code with colours so that it graphs those arrows based on the colour I specified. The colours that match the arrows are important which is why I did it that way. I am having trouble now adding a legend as ggplot isn't adding a legend.
Is there a way to add one or do I have to do something to the dataframe?
I was thinking of adding the colours manually, but I am getting stuck.
Green represents Sulfated, Orange represents Sialyllated, and Brown represents Neutral. And I would like the legend to show that.
Here is the code:
Dataframe
rrload<-data.frame(rr.pr$rotation[c(2,15,17,24,52),c(1:5)])
rrload$class<-c('orange','springgreen3','bisque3','bisque3','bisque3')
rrload1<-rrload[,c(1:5)]
rrload1<-as.numeric(as.matrix(rrload1))
rrload1<-matrix(rrload1,nrow=5,ncol=5,byrow = F)
rrload[,c(1:5)]<-rrload1
Code for plotting it:
ggplot(rrload)+geom_segment(aes(xend=PC1,yend=PC2),x=0,y=0,arrow = arrowstyle2,color=rrload$class)+
geom_text(aes(x=PC1,y=PC2,label=row.names(rrload)),hjust=0,nudge_x = -0.05,vjust=1,nudge_y = 0.025,size=3.5,color='black')+xlim(-0.3,0.3)+ylim(-0.3,0.3)+theme_light()+
theme_minimal()+theme(legend.title = element_text("Class"),axis.text.x = element_text(colour = "black",size = 10),axis.text.y = element_text(colour = "black",size = 10),axis.title.x = element_text(colour = "black",size = 10),axis.title.y = element_text(colour = "black",size = 10),axis.ticks = element_line(color = "black"),panel.grid = element_blank(), panel.border = element_rect(colour = "black",fill = NA,size = 1))+geom_hline(yintercept = 0,linetype="dashed",color="gray69")+geom_vline(xintercept = 0,linetype="dashed",color="gray69")
This is the graph:
Loadings plot
Without access to your full data (your code is unable to recreate the dataframe, rrload properly), it's hard to help. I managed to estimate the numbers based on the plot you shared. Here's the dataframe I used - note the naming conventions for the columns:
d <- data.frame(
PC1=c(-0.2,-0.2,0.1,0.15,-0.08),
PC2=c(0.13,-0.1,0.2,0.1,-0.2300),
class=c('Neutral','Neutral','Neutral','Sulfated','Silylated'),
name=c('o53','o18','o25','o15','o2')
)
To prepare the data for plotting, I included d$name and d$class. d$class is similar to the column you had, although instead of the color, I'm using the actual name. d$name is the name that I'm using to plot your labels.
Here's the code I used and resulting plot. Explanation will come after:
library(ggrepel)
ggplot(d) + theme_classic() +
geom_vline(xintercept=0, linetype=2, color='gray60') +
geom_hline(yintercept=0, linetype=2, color='gray60') +
geom_segment(
aes(xend=PC1,yend=PC2, color=class), x=0,y=0,
arrow=arrow(type='closed', angle=20, length=unit(0.02,'npc'))
) +
geom_text_repel(
aes(x=PC1, y=PC2, label=name), force=6, min.segment.length = 10, seed=123
) +
ylim(-0.3,0.3) + xlim(-0.3,0.3) +
scale_color_manual(
name='Legend Title',
values=c('Neutral'='bisque3','Sulfated'='springgreen3','Silylated'='orange'))
ggplot2 will create a legend for certain aesthetics, but they must be placed within aes(). Once you do that, ggplot2 will create the legend and automatically assign colors. This means that if we want to create a legend for color=, you need to put it within aes(). The interesting part is that you can put it within aes() anywhere in the call, or just apply to specific geom/geoms. This allows a lot of flexibility in creating your plot. In this case, I only want to color the arrows, so you include color=class within the geom_segment() call. If you put it within the ggplot() call, it would color both the line segment as well as the text geom.
I'm also paying attention to the ordering. We want to make sure the background dotted lines for the central axis at 0,0 are "behind" everything, so they go first. Then the segments, and then the text geom.
The scale_color_manual() function is used to specify the colors for the different d$class values explicitly and the name of the legend. You can also just let ggplot2 find a palette by default, or you can specify via a palette (there are a ton of other methods to specify color). BTW - you can also specify the name of the legend via labs(color=....
Finally, I decided to use geom_text_repel() rather than geom_text(). Since the lines go out in every direction, the "nudge" values for each text item are not going to work going in the same direction. In other words, if you plot the text at x=PC1, y=PC2, it will overlap the arrowheads. You noticed this too and applied nudge_ values, which happens to work, but if your data was a bit different, it would not have worked. geom_text_repel from the ggrepel package can work to do this by kind of "pushing" the text away from your points.
I have made plots in R (RStudio) with ggplot2. When I export them via export::graph2office, the labels are moved around. However, this only happens when I specify the font for the labels.
library (ggplot2)
library (export)
plot_data <- data.frame (a = runif (1:20), b = seq (1:20))
x11 (width = 3, height = 3)
ggplot (data = plot_data, mapping = aes (x = a, y = b)) +
geom_point () +
labs (x = "my x-label", y = "my y-label") +
theme (panel.background = element_blank(),
panel.border = element_rect (fill = NA, size = 0.7),
axis.ticks = element_line (color = "black", lineend = "round"),
axis.ticks.length = unit (2, "mm"),
axis.text = element_text (color = "black"),
plot.margin = unit(rep (0, 4), "cm"),
text = element_text (size=18,
family="ChantillyLH",
color = "black")
)
graph2office (file = "my_graph", type = "DOC")
Here, you can see the graph in R (to the right) and the exported graph in word (to the left):
The undesired behaviour is more obvious for the y-label in this example, but also the x-label is moved a bit. I wonder if there is a way to fix this.
The same happens when I specify another font family, for example family="Comic Sans MS":
EDIT: it even happens when no textcommand is given:
The answer probably is: yes, export::graph2office moves axis labels around (so do export::graph2pptand export::graph2doc). There is no way to fix this. If you want to style your graphs in R and export them as-is into Office, the export::graph2office function, unfortunately, is not your way to go. However, the function can of course be used as a quick-and-dirty option to produce editable office-graphs.
If your goal is to export graphs in a more reliable manner, CairoSVG might be a much better option (see my answer here: Producing a vector graphics image (i.e. metafile) in R suitable for printing in Word 2007).
I have a number of maps that I'm generating in R using the sf library and I would like to have a nice looking legend. Unfortunately, it seems that the standard legend for geom_sf() are these ugly looking boxes. The only SO post I could find on adjusting shapes in ggplot2 legends is here.
The here is to use guides(colour = guide_legend(override.aes = list(shape = 16))); however, this only seems to work for geom_point() and not for geom_sf().
Unless someone can suggest an alternative method for changing the shape of legend objects I will need to design a custom legend in Inkscape and align this along with various maps.
Here's a snippet of the code to show what I've tried already:
legend <- image_read_svg('https://svgshare.com/i/FDV.svg')
p1 <- ggplot() +
geom_sf(data=otherroads, size = .45, aes(color=SUFTYPABRV)) +
geom_sf(data=allroads, size = .55, aes(color=SUFTYPABRV)) +
scale_color_manual(values = plotcolors, guide = "legend") + theme_map() +
labs(title = "Sydney")
ggdraw() +
draw_plot(p1) +
draw_image(legend, width = 0.4, hjust = -0.75, vjust = 0.43)
Good legend location example
The output looks good in this case; however, this won't work for me because it requires manual tweaking for every plot.
What I would like is for the location of this legend to be dynamically placed according to the ggplot object, which depends upon the city I'm plotting.
Bad legend location example
The code (and data) in it's entirety can be cloned from github: https://github.com/moldach/map-help.git
This answer is not to adress the legend placement, but changing the legend icons. From your examples, I gather your data produces a legend that looks like this by default:
# example from the geom_sf help page
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
# throwing in some random categorical data
nc$catvar <- sample(LETTERS[1:5], nrow(nc), replace = TRUE)
ggplot(nc) +
geom_sf(aes(colour = catvar))
Not too long ago, ggplot added the ability to set what legend shape you wanted by implicitly (via the ellipsis) adding the key_glyph argument to all layers, such as geoms and stats.
ggplot(nc) +
geom_sf(aes(colour = catvar), key_glyph = "timeseries")
You could use this, to set the glyph to points and then use the override.aes trick to get the shapes that you want.
ggplot(nc) +
geom_sf(aes(colour = catvar), key_glyph = "point") +
guides(colour = guide_legend(override.aes = list(size = 3, shape = c(15:19))))
It should work with all the regular legend placement tools that are already in ggplot. You might have to specify locations specific for the plot, but at least you won't have to export your legend to svg files first before adding them.
I'd like to use unicode characters as the shape of plots in ggplot, but for unknown reason they're not rendering. I did find a similar query here, but I can't make the example there work either.
Any clues as to why?
Note that I don't want to use the unicode character as a "palette", I want each item plotted by geom_point() to be the same shape (color will indicate the relevant variable).
Running
Sys.setenv(LANG = "en_US.UTF-8")
and restarting R does not help. Wrapping the unicode in sprintf() also does not help.
This is an example bit of code that illustrates the problem:
library(tidyverse)
library(ggplot2)
library(Unicode)
p1 = ggplot(mtcars, aes(wt, mpg)) +
geom_point(shape="\u25D2", colour="red", size=3) +
geom_point(shape="\u25D3", colour="blue", size=3) +
theme_bw()
plot(p1)
And here's what that renders result.
I use macOS Sierra (10.13.6), R version 3.5.1 & Rstudio 1.0.143.
Grateful for any help! I've been scouting several forums looking for a solution and posted to #Rstats, so far nothing has worked. It may be that the solution is hidden in some thread somewhere, but if so I have failed to detect it and I suspect others have also missed it. So, here I am making my first ever post to stack overflow :)
Might it work to use geom_text instead? It allows control of the font, so you can select one with the glyph you need.
library(tidyverse)
ggplot(mtcars, aes(wt, mpg)) +
geom_text(label = "\u25D2", aes(color = as.character(gear)),
size=10, family = "Arial Unicode MS") +
geom_text(label = "\u25D3", colour="blue",
size=10, family = "Arial Unicode MS") +
scale_color_discrete(name = "gear") +
theme_bw()
It's possible to change the font family using par. The problem is that this will affect base R graphics but not ggplot2 graphics, as they use two different graphics devices (grDevices vs. grid). For instance, we can try to plot your example using base R functions, but at first we see the same issue:
plot(mtcars$wt, mtcars$mpg, pch="\u25D2", col = "red", cex = 2)
points(mtcars$wt, mtcars$mpg, pch="\u25D3", col = "blue", cex = 2)
We can get what we want if we call par first (the font should support the symbols):
par(family = "Arial Unicode MS")
plot(mtcars$wt, mtcars$mpg, pch="\u25D2", col = "red", cex = 2)
points(mtcars$wt, mtcars$mpg, pch="\u25D3", col = "blue", cex = 2)
Changing the font family parameter that specifically affects the points in a ggplot geom_point appears to be a bit more complicated. As far as I can tell, it would involve turning the ggplot object into a grob, editing the parameters, and then drawing it. It probably makes more sense to either use Jon Spring's geom_text solution, or use base R.
I have been using filled symbols in ggplot2 and never had any problems exporting to png. However, when I export to svg, for some shapes (triangles and diamonds) one side of the symbol has no stroke.
This initially occurred when I was plotting some maps, so I have tried to replicate the issue with just simple example plots - it's still happening. I can't fathom what is causing one side of the symbols to disappear like this. Is this something I am failing to specify when exporting as svg? Or is there a bug somewhere? Any help would be much appreciated.
Here's an example:
And the code that created this image:
library(tidyverse)
plot =
data_frame(x = 1:5, y = 1:5, group = c("tri", "sq", "tri", "sq", "dia")) %>%
ggplot(aes(x,y, shape = group)) +
geom_point(fill = "red", colour = "black", size = 4) +
scale_shape_manual(values = c(23,22,24)) +
theme_bw()
plot %>% ggsave("test.svg", ., height = 10, width = 10, units = "cm")
Note: I have tried using svglite() directly to export - same thing happens.
This seems like a bug in svglite:
Missing edges in svg file for some point characters when background color is set
First reported on ggplot2 github:
ggsave missing edges with some shapes in svg format