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
Related
I am tring to create plotly bar plot with scalling colors.
I have found out how to adjust color scaling by variable, but don't know how to choose another pallet. I would like to use RBrewerPallet for example.
Here is an example.
library(plotly)
library(dplyr)
airquality %>% group_by(Month) %>% summarise(total = sum(Wind)) %>%
plot_ly(x = ~Month,
y = ~total,
type = 'bar',
marker = list(
color = ~total
))
Here is a way to specify the color scale for bar charts in plotly:
library(plotly)
library(dplyr)
airquality %>% group_by(Month) %>% summarise(total = sum(Wind)) %>%
plot_ly(x = ~Month,
y = ~total,
type = 'bar',
marker = list(
color = ~total,
colorscale='Blues'
))
I know of these palletes: "Blackbody", "Bluered", "Blues", "Earth", "Electric", "Greens", "Greys", "Hot", "Jet", "Picnic", "Portland", "Rainbow", "RdBu", "Reds", "Viridis", "YlGnBu", "YlOrRd".
if not enough you can manually specify:
marker = list(color = c('rgba(204,204,204,1)', 'rgba(222,45,38,0.8)',
'rgba(204,204,204,1)', 'rgba(204,204,204,1)',
'rgba(204,204,204,1)')
I'd like to adjust the tick labels in a plotly chart so that they would display a comma as a decimal separator and a point as a thousand separator.
library(plotly)
library(ggplot2)
library(dplyr)
diamonds %>%
mutate(cut = as.character(cut)) %>%
count(cut, clarity) %>%
plot_ly(x = ~cut, y = ~n, color = ~clarity) %>%
layout(yaxis = list(tickformat = ",.1f"))
my local is already set to "LC_COLLATE=German_Austria.1252;LC_CTYPE=German_Austria.1252;LC_MONETARY=German_Austria.1252;LC_NUMERIC=C;LC_TIME=C"
This is an ugly answer but you can set up your object:
d2 <- diamonds %>%
mutate(cut = as.character(cut)) %>%
count(cut, clarity)
and then create the axis labels from there:
ticklabels <- seq(from=0, to=round(max(d2$n), digits = -3), by=1000)
To create a custom axis label:
plot_ly(d2, x = ~cut, y = ~n, color = ~clarity) %>%
layout(yaxis = list(tickvals = ticklabels, ticktext = paste(ticklabels/1000, ".000", ",00", sep="") ))
This thread shows how to change the position of the legend in a ggvis object. However, if tooltips are added as well, the legend disappears.
library(ggvis)
data(mtcars)
mtcars %>%
ggvis(x = ~wt, y = ~mpg, fill = ~cyl) %>%
layer_points() %>%
add_legend(
"fill",
properties = legend_props(
legend = list(
x = scaled_value("x", 3.25),
y = scaled_value("y", 40)
)
)
) %>%
add_tooltip(function(df) df$wt)
Any idea how to prevent this?
This is a know issue, you can try adding the below at the end of your code as a workaround:
... %>% set_options(duration = 0)
This is what i have so far with the ggvis package in R.
mtcars %>% ggvis(x = ~disp) %>%
layer_lines(y = ~wt, stroke := "red") %>%
layer_lines(y = ~mpg) %>%
add_axis("y", orient = "left", title = "Weight (lb/1000)") %>%
add_axis("y", orient = "right", title= "Miles/(US) gallon") %>%
add_axis("x", title = "Displacement (cu.in.)")
I cannot get the left Y axis to represent the wt scale data.
This outputs:
I assume you want the left y axis (i.e. wt) divided by 1000:
library(dplyr) #you need this library
mtcars %>% mutate(wt_scaled=wt/1000) %>% ggvis(x = ~disp) %>% #use mutate from dplyr to add scaled wt
layer_lines(y = ~wt_scaled, stroke := "red") %>% #use new column
add_axis("y", orient = "left", title = "Weight (lb/1000)" ,title_offset = 50) %>% #fix left axis label
scale_numeric("y", domain = c(0, 0.006), nice = FALSE) %>% #align the ticks as good as possible
add_axis("y", 'ympg' , orient = "right", title= "Miles/(US) gallon" , grid=F ) %>% #remove right y axis grid and name axis
layer_lines( prop('y' , ~mpg, scale='ympg') ) %>% #use scale to show layer_lines which axis it should use
add_axis("x", title = "Displacement (cu.in.)" )
and I think this is what you want:
EDIT:
If you just wanted to plot wt on the left y axis (it is not very clear) then do mutate(wt_scaled=wt/1) (or remove mutate) and change domain to domain = c(1.5, 5.5)
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")