Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrowhead used as a size aesthetic in ggplot2

Tags:

r

ggplot2

I've got data containing vectors' coordinates and their characteristics - success_rate and prop:

ex_data <- structure(list(group = c("group A", "group A", "group A", "group A", 
"group A", "group A", "group C", "group C", "group C", "group C", 
"group C", "group W", "group W", "group W", "group W", "group W"
), category = c(5, 4, 3, 2, 1, 6, 5, 1, 2, 3, 4, 1, 4, 5, 2, 
3), success_rate = c(0.816037735849057, 0.938775510204082, 0.653061224489796, 
0.985915492957746, 0.934306569343066, 1, 0.979166666666667, 0.887323943661972, 
0.319587628865979, 0.721590909090909, 0.941176470588235, 0.689320388349515, 
0.338028169014085, 0.396551724137931, 0.7375, 0.763948497854077
), x0 = c(24.5, 24.5, 23.7, 22.6, 28.6, 27.6, 27.2, 27.4, 15.7, 
25.5, 24.2, 21.4, 17.5, 9.9, 23.6, 29), y0 = c(21.2, 27.9, 96.6, 
43.9, 65.1, 71.5, 34.3, 71.2, 47.9, 88, 36, 86.9, 49.4, 41.3, 
85.6, 16.7), x1 = c(35.2, 36.5, 34, 32.9, 39.4, 40.2, 35.9, 31.6, 
63.1, 29.5, 35.9, 35.4, 61.4, 54, 37.3, 37.2), y1 = c(5.8, 33.6, 
96.8, 70, 64.8, 96.6, 7.3, 64.9, 63.1, 89.7, 38.5, 95, 59.3, 
32.2, 77.9, 12), prop = c(0.124926340601061, 0.0866234531526223, 
0.0288744843842074, 0.041838538597525, 0.0807307012374779, 0.0271066588096641, 
0.0365853658536585, 0.0541158536585366, 0.0739329268292683, 0.134146341463415, 
0.0259146341463415, 0.0787461773700306, 0.108562691131498, 0.0443425076452599, 
0.0611620795107034, 0.178134556574924)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

I wan't to make a graphic of vectors for each group that would use prop as a size aesthetic. Unfortunetely, I don't think in case of arrows size aesthetics doesn't work well as its resizing the whole arrow which often look bad:

ggplot(ex_data) +
  geom_segment(aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate, size = prop),
               arrow = arrow(length = unit(0.2, "cm"), ends = 'last', type = 'closed')) +
  facet_wrap(~group) +
  scale_color_viridis_c()

enter image description here

So I would like to control only the size of the arrowhead with prop variable. Unfortunetely, arrowheads are not geom_segment aesthetic and the only way to control their sizes is to supply them with separate vector. It works nice for a single group:

ex_data %>% 
  filter(group == 'group C') %>% 
  ggplot() +
  geom_segment(aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate),
               arrow = arrow(length = unit(filter(ex_data, group == 'group C')$prop * 3, "cm"), ends = 'last', type = 'closed')) +
  facet_wrap(~group) +
  scale_color_viridis_c()

enter image description here

but not with facet_wrap:

ggplot(ex_data) +
  geom_segment(aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate),
               arrow = arrow(length = unit(ex_data$prop * 3, "cm"), ends = 'last', type = 'closed')) +
  facet_wrap(~group) +
  scale_color_viridis_c()

enter image description here

You can notice that sizes of arrowheads for group C are not valid anymore (eg. the upper arrow should have the biggest arrowhead, while it has the smallest). My original data has much more groups and creating separate plots would be really tedious. Is there any workaround for this?

like image 666
Kuba_ Avatar asked Jan 24 '19 12:01

Kuba_


People also ask

Is it possible to show two size aesthetics on one ggplot2?

From what I can find on stackoverflow, (such as this answer to using two scale colour gradients on one ggplot) this may not (yet) be possible with ggplot2. I want to create a bubbleplot with two size aesthetics, one always larger than the other. The idea is to show the proportion as well as the absolute values.

What do I need to know about aesthetic scales in ggplot?

You will need some knowledge of the valid aesthetic values, which are described in vignette ("ggplot2-specs") . Identity scales — such as scale_colour_identity () and scale_shape_identity () — are used when your data is already scaled such that the data and aesthetic spaces are the same.

How do I set font size in ggplot2?

Typically you specify font size using points (or pt for short), where 1 pt = 0.35mm. ggplot2 provides this conversion factor in the variable.pt, so if you want to draw 12pt text, set size = 12 /.pt.

What is the fill argument in ggplot2?

All the data sets used in this chapter can be found here and code can be downloaded from here. In ggplot2, when we mention color or colour, it usually refers to the color of the geoms. The fill argument is used to specify the color of the shapes in certain cases.


1 Answers

As is usual in such cases (e.g., 1, 2, 3), we may use sub-data frames for each facet. For instance,

ggplot(ex_data) +
  lapply(split(ex_data, ex_data$group), function(df)
  geom_segment(data = df, aes(x = x0, y = y0, xend = x1, yend = y1, color = success_rate),
               arrow = arrow(length = unit(df$prop * 3, "cm"), ends = 'last', type = 'closed'))) +
  facet_wrap(~group) + scale_color_viridis_c()

enter image description here

like image 151
Julius Vainora Avatar answered Sep 19 '22 12:09

Julius Vainora