How to change background color in ggvis? - r

require(ggvis)
require(dplyr)
map_data = ggplot2::map_data("world")
map_data %>% select(long, lat, group) %>%
group_by(group) %>%
ggvis(x = ~long, y = ~lat) %>% layer_paths(fill:="#666666") %>%
hide_axis("x") %>% hide_axis("y")
That produces a nice map, but I'd like control over the "ocean" color (e.g. "black"). How to do that?
More generally, the ocean color is the plot default background.

Adding a layer_rects seems to have done it:
require(ggvis)
require(dplyr)
map_data = ggplot2::map_data("world")
minx = min(map_data$long -1)
maxx = max(map_data$long +1)
miny = min(map_data$lat - 1)
maxy = max(map_data$lat + 1)
map_data %>% select(long, lat, group) %>%
group_by(group) %>%
ggvis(x = ~long, y = ~lat) %>%
layer_rects(x=minx, x2=maxx, y=maxy, y2=miny, fill:="#000000") %>%
layer_paths(fill:="#666666") %>%
hide_axis("x") %>% hide_axis("y")

Related

echarts4r: fix axis range across groups

I am creating a grouped bar chart like so:
library(tidyverse)
library(echarts4r)
data("starwars")
starwars %>%
group_by(sex, eye_color) %>%
summarise(height = mean(height, na.rm=TRUE)) %>%
group_by(sex) %>%
e_charts(x = eye_color, timeline = TRUE) %>%
e_bar(height, legend = FALSE)
How do I set the range of the y axis (height) to be the same across groups (sex)?
You could set maximum value for the y axis using e_y_axis(max = XXX), e.g. in the code below I set the max value based on the maximum of height.
library(tidyverse)
library(echarts4r)
data("starwars")
ymax <- max()
dat <- starwars %>%
group_by(sex, eye_color) %>%
summarise(height = mean(height, na.rm=TRUE), .groups = "drop")
ymax <- 50 * ceiling(max(dat$height, na.rm = TRUE) / 50)
dat %>%
group_by(sex) %>%
e_charts(x = eye_color, timeline = TRUE) %>%
e_bar(height, legend = FALSE) %>%
e_y_axis(max = ymax)

How to duplicate axis in highcharter

How do I duplicate my x-axis (becomes y in type='bar'?) so I have them both at the top and bottom?
Example:
library(dplyr)
library(ggplot2)
library(highcharter)
mpgg <- mpg %>%
filter(class %in% c("suv", "compact", "midsize")) %>%
add_row(manufacturer="loner",class="newClass")%>%
group_by(class, manufacturer) %>%
summarize(count = n())
categories_grouped <- mpgg %>%
group_by(name = class) %>%
do(categories = .$manufacturer) %>%
list_parse()
highchart() %>%
hc_xAxis(categories = categories_grouped) %>%
hc_add_series(data = mpgg, type = "bar", hcaes(y = count, color = manufacturer),
showInLegend = FALSE)

how to plot lines matching data using ggplot2

plot increase_rate contains abs(increase_rate) > 0.05.
but under the code, discard the data between -0.05 to 0.05.
I also plot data including from -0.05 to 0.05 range.
library(tidyverse)
data(population, package="tidyr")
population %>%
arrange(country, year) %>%
group_by(country) %>%
mutate(population_nextY = lead(population)) %>%
mutate(increase_rate = (population_nextY - population)/population_nextY) %>%
filter(abs(increase_rate) > 0.05) %>%
ungroup %>%
ggplot()+
geom_line(aes(x = year, y = increase_rate, color = country))
I want to get final plot like this.
d <-
population %>%
arrange(country, year) %>%
group_by(country) %>%
mutate(population_nextY = lead(population)) %>%
mutate(increase_rate = (population_nextY - population)/population_nextY) %>%
ungroup
select_country <-
d %>% filter(!between(increase_rate, -0.05, 0.05)) %>%
select(country) %>% distinct %>% unlist
d %>%
filter(country %in% select_country) %>%
ggplot()+
geom_line(aes(x = year, y = increase_rate, color = country))
use between:
filter(!between(increase_rate, -0.05, 0.05))
add column using mutate() function
population %>%
arrange(country, year) %>%
group_by(country) %>%
mutate(population_nextY = lead(population)) %>%
mutate(increase_rate = (population_nextY - population)/population_nextY) %>%
mutate(judge = max(abs(increase_rate), na.rm=T)) %>%
filter(judge > 0.05) %>%
ungroup %>%
ggplot() +
geom_line(aes(x = year, y = increase_rate, color = country))

Why the "y" prop cannot be set in layer_smooths in ggvis?

This works fine:
mtcars %>% ggvis(x = ~wt) %>% layer_lines(y = ~mpg)
But this:
mtcars %>% ggvis(x = ~wt) %>% layer_smooths(y = ~mpg)
gives an error: "Error: Can't find prop y.update."
Maybe this is what you are looking for:
mtcars %>% ggvis(x = ~wt, y = ~mpg) %>% layer_smooths()

ggvis add_legend for 'strokeDash' or 'strokeWidth'

I want to group data on two variables and distinguish them in a ggvis plot via 'stroke' and 'strokeDash'. If I want to add legends to a ggvis plot, I can do so for the 'stroke' property:
library(ggvis)
mtcars %>%
ggvis(~wt, ~mpg, stroke = ~factor(cyl), strokeDash = ~factor(vs)) %>%
layer_lines() %>%
add_legend('stroke', title = 'Number of cylinders')
However, if try to add a legend for 'strokeDash', the plot does not get rendered:
mtcars %>%
ggvis(~wt, ~mpg, stroke = ~factor(cyl), strokeDash = ~factor(vs)) %>%
layer_lines() %>%
add_legend('stroke', title = 'Number of cylinders') %>%
add_legend('strokeDash', title = 'V/S')
Same applies, if I try it with 'strokeWidth' instead of 'strokeDash':
# works
mtcars %>%
ggvis(~wt, ~mpg, stroke = ~factor(cyl), strokeWidth = ~factor(vs)) %>%
layer_lines() %>%
scale_nominal('strokeWidth', range = c(1,5)) %>%
add_legend('stroke', title = 'Number of cylinders')
# does not work
mtcars %>%
ggvis(~wt, ~mpg, stroke = ~factor(cyl), strokeWidth = ~factor(vs)) %>%
layer_lines() %>%
scale_nominal('strokeWidth', range = c(1,5)) %>%
add_legend('stroke', title = 'Number of cylinders')%>%
add_legend('strokeWidth', title = 'V/S')
Does anyone know, why for 'stroke' it works and for the others it does not? Any solutions?
Thanks,
Fabian

Resources