r - combine bar and scatterplot in ggplot -


i have data topic, if customers talk positive, negative or neutral , topics impact , difference average.

data <- data.frame(topic = c("a", "b", "c", "d"), impact = runif(4, min = -2, max = 2),                 difference = runif(4, min = -30, max = 30),n = round(runif(4, min = 1, max = 100)),                 share_positive = c(0.04, 0.9, 0.3, 0.37), share_neutral = c(0.7, 0.06, 0.48, 0.4),                 share_negative = c(0.26, 0.04, 0.22, 0.23)) 

i put in simple scatterplot:

ggplot(data = data, aes(x = difference, y = impact, size = n, label = topic)) +   geom_point() +   geom_text(size = 4, vjust = -1, colour = "black") 

then added colour distinct between positive , less positive topics:

ggplot(data = data, aes(x = difference, y = impact, size = n, label = topic, colour = share_positive)) +   geom_point() +   scale_color_continuous(low = "grey", high = "green", guide = false) +   geom_text(size = 4, vjust = -1, colour = "black") 

instead of colouring, nice have bars indicate share of negative, neutral , positive feedback in topic. idea (example b , d):

enter image description here

unfortunately, have absolutely no idea, how can combine , if it's possible. googeling doesn't either. can me?

here's way it, it's sort of "hacky". use difference column , share_* columns manually define xmin , xmax should when call geom_rect. make sure bar, add little fudge factor (0.1) ymax

library(tidyverse) theme_set(theme_bw())  data %>%     mutate(neg_left = difference - share_negative,            neutral_right = difference + share_neutral,            positive_right = neutral_right + share_positive) %>%     ggplot(., aes(ymin = impact, ymax = impact + .1))+     geom_text(aes(x = difference, y = impact, label = topic), vjust = 1)+     geom_rect(aes(xmin = neg_left, xmax = difference), fill = 'red', colour = 'black')+     geom_rect(aes(xmin = difference, xmax = neutral_right), fill = 'grey', colour = 'black')+     geom_rect(aes(xmin = neutral_right, xmax = positive_right), fill = 'green', colour = 'black') 

enter image description here

data (using set.seed reproducibility)

set.seed(456) data <- data.frame(topic = c("a", "b", "c", "d"), impact = runif(4, min = -2, max = 2),                     difference = runif(4, min = -30, max = 30),n = round(runif(4, min = 1, max = 100)),                     share_positive = c(0.04, 0.9, 0.3, 0.37), share_neutral = c(0.7, 0.06, 0.48, 0.4),                     share_negative = c(0.26, 0.04, 0.22, 0.23)) 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -