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()
Related
I have the following code to create a simple column plot and it works fine:
library(highcharter)
d1 <- iris %>% group_by(Species) %>%
summarize(mean_sepal_width = mean(Sepal.Width))
highchart() %>%
hc_chart(type = 'column') %>%
hc_xAxis(categories = d1$Species) %>%
hc_add_series(data = d1$mean_sepal_width)
However, when I subset the input data such that only a single x variable exists, the x axis labels are broken:
d2 <- d %>% filter(Species == 'virginica')
highchart() %>%
hc_chart(type = 'column') %>%
hc_xAxis(categories = d2$Species) %>%
hc_add_series(data = d2$mean_sepal_width)
A potential solution is offered here (Highcharter bar chart cut off x axis label) but I prefer not to use the hchart() function since my actual plot is a lot more complicated.
Is there a way to fix these x axis labels?
Put d2$Species in a list (or use as.list). This is a known bug.
highchart() %>%
hc_chart(type = 'column') %>%
hc_xAxis(categories = as.list(d2$Species)) %>%
hc_add_series(data = d2$mean_sepal_width)
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)
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))
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
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")