R Hexadecimal colours do not work in ggvis - r

I have been fiddling around with ggvis and ran into this problem that I cannot use hexadecimal values for colours like regular R plots. Is this normal behaviour for ggvis or am I missing something?
library(ggvis)
# create data frame
df <- data.frame(var1=1:10,var2=5:14,var3=factor(c(rep("A",5),rep("B",5))))
# code without custom colours
df %>%
ggvis(x = ~var1, y = ~var2,fill = ~var3) %>%
layer_points(size := 30,fillOpacity := 0.5)
#code with custom colours as text
df %>%
ggvis(x = ~var1, y = ~var2,fill = ~var3) %>%
layer_points(size := 30,fillOpacity := 0.5) %>%
scale_nominal("fill",range=c("red","green"))
# code with custom colours as hexadecimal values
df %>%
ggvis(x = ~var1, y = ~var2,fill = ~var3) %>%
layer_points(size := 30,fillOpacity := 0.5) %>%
scale_nominal("fill",range=c("#0000FFFF","#FF3300FF"))

The 8-character codes you're using include 2 extra characters at the end, which, as you mentioned, control transparency. While these may be recognized in base R, ggvis is expecting a traditional hex code, i.e. one with 6 characters. To get the hex codes you need, simply omit the last 2 characters of your current color codes. Transparency is controlled separately through parameters passed to the ggvis family of functions.

Related

ggplot2 heatmap total number as Fill value and two

I have a table with pokemon data that can be found in Kaggle: Link
I'm trying to produce a heatmap using ggplot2 but can't figure out how to use the sum of each pokemon type in each generation as the fill value. The total value should be calculated from two columns, "Type" and "Other Type"
This is what I tried but it doesn't seem to work.
ggplot(pokemon_mod, aes(x= Generation, y= Type, z= (Type, Other.Type)) +
geom_tile()
One issue in your code is that the color of the tile is specified with the fill aesthetic, not z. Also in general it's better to do feature engineering outside of ggplot2 and then pass the data in.
Your {dplyr} syntax from the comment is not quite right, but you're close with count().
With dplyr::count() you don't need to first group_by() so it saves you a step (it's shorthand for dplyr::group_by(...) %>% dplyr::summarize(count = n()).
If you want to just combine the counts of Type and Other Type, you can concatenate into a new column and then use tidyr::separate_rows() to essentially append them. Then you just have to remove the "NA" values and I think you'll get what you're after:
library(tidyverse)
library(vroom)
d <- vroom("pokemon-data.csv") # downloaded from [Kaggle](https://www.kaggle.com/datasets/swashbuckler1/pokemon-gen1gen8?resource=download)
d %>%
mutate(types = paste(Type, `Other Type`, sep = "_")) %>%
separate_rows(types, sep = "_") %>%
count(Generation, types) %>%
filter(types != "NA") %>%
ggplot(aes(Generation, types)) +
geom_tile(aes(fill = n)) +
scale_x_continuous(breaks = 1:8)
Created on 2022-11-09 with reprex v2.0.2

e_facet using grouped data in echarts4r question

I really like the possibilities this package offers and would like to use it in a shiny app. however i am struggling to recreate a plot from ggplot to echarts4r
library(tidyverse)
library(echarts4r)
data = tibble(time = factor(sort(rep(c(4,8,24), 30)), levels = c(4,8,24)),
dose = factor(rep(c(1,2,3), 30), levels = c(1,2,3)),
id = rep(sort(rep(LETTERS[1:10], 3)),3),
y = rnorm(n = 90, mean = 5, sd = 3))
This is the plot i am aiming to recreate:
ggplot(data = data, mapping = aes(x = time, y = y, group = id)) +
geom_point() +
geom_line() +
facet_wrap(~dose)
The problem i am having is to make groups of my data using group = id in ggplot syntax in echarts4r . I am aiming to do e_facet on grouped data using group_by() however i can not (or dont know how to) add a group to connect the dots using geom_line()
data %>%
group_by(dose) %>%
e_charts(time) %>%
e_line(y) %>%
e_facet(rows = 1, cols = 3)
You can do this with echarts4r.
There are two methods that I know of that work, one uses e_list. I think that method would make this more complicated than it needs to be, though.
It might be useful to know that e_facet, e_arrange, and e_grid all fall under echarts grid functionality—you know, sort of like everything that ggplot2 does falls under base R's grid.
I used group_split from dplyr and imap from purrr to create the faceted graph. You'll notice that I didn't use e_facet due to its constraints.
group_split is interchangeable with base R's split and either could have been used.
I used imap so I could map over the groups and have the benefit of using an index. If you're familiar with the use of enumerate in a Python for statement or a forEach in Javascript, this sort of works the same way. In the map call, j is a data frame; k is an index value. I appended the additional arguments needed for e_arrange, then made the plot.
library(tidyverse) # has both dplyr and purrrrrr (how many r's?)
library(echarts4r)
data %>% group_split(dose) %>%
imap(function(j, k) {
j %>% group_by(id) %>%
e_charts(time, name = paste0("chart_", k)) %>%
e_line(y, name = paste0("Dose ", k)) %>%
e_color(color = "black")
}) %>% append(c(rows = 1, cols = 3)) %>%
do.call(e_arrange, .)

R ggplot points and color by groups: show multiple color if a point is in multiple groups?

I'm using ggplot to color my points by group and trying to see if there's a way in ggplot to have a point filled with two or more colors if that point belongs to more than one group.
My example dataset looks like this:
df1<-data.frame("lat"=c(0,0,0,0,10,10,10,10,20,20,20,20,20,20),
"long"=c(100,110,120,120,100,110,110,120,100,100,100,110,110,120),
"type"=c("A","B","A","B","C","A","C","A","A","B","C","B","C","B"))
If you look at the dataset, you can see that some points only have one type, while others have multiple types (for instance, at lat=0 and long=120, type=A,B).
My plot right now looks like this:
ggplot(df1,aes(x=df1$lat,y=df1$long,col=df1$type))+geom_point(shape=1)
This colors each lat and long point with type, but at lat=0 and long=120 I only see type B color because it overwrote type A color.
I'm wondering if it's possible to have a point have two boundaries (inner boundary with type A color and outer boundary with type B color), or if I can divide the boundary into two so that left half of it shows type A color and the right half of it shows type B color, or any other method that would have each point show multiple colors if they have more than one type.
I'd prefer being able to draw multiple boundaries for sake of visualization but really any other suggestions or insight would be really helpful!
You can try adding a new variable to your data and passing that as the size:
library(dplyr)
library(ggplot2)
df1 %>%
group_by(lat, long) %>%
arrange(type, .by_group = TRUE) %>%
mutate(size = row_number(), n = n()) %>%
ungroup() %>%
mutate(size = max(n) - size) %>%
ggplot(aes(x = lat, y = long, color = type, size = size)) +
geom_point(shape = 1) +
scale_size_continuous(range = c(3, 8), guide = FALSE)

Scatterplot based on 2 categorical variables using ggvis package in R

I need to get a scatter plot like this based on 2 categorical variables where each variable has 2 levels.
I am using ggvis package in R.
This is my code so far
salab<- read.table("http://users.stat.ufl.edu
/~rrandles/sta4210/Rclassnotes/data/textdatasets/KutnerData/
Chapter%2022%20Data%20Sets/CH22TA06.txt", quote="\"", comment.char="")
salab %>% ggvis(~V2, ~V1, fill = ~factor(V3*V4)) %>% layer_points()
Which is incorrect because i need 4 factors combinations. Can anyone help me to figure out what modification should i do ?
I think you need factor(V3):factor(V4) instead of factor(V3*V4):
salab %>%
ggvis(~V2, ~V1, fill = ~ factor(V3):factor(V4)) %>%
layer_points()
An alternative:
salab$`V3*V4`<-paste0("V3=",salab$V3,"*","V4=",salab$V4)
salab %>% ggvis(~V2, ~V1, shape = ~`V3*V4`) %>% layer_points()

Lines stroke (color) as legend and moving legend labels next to lines

I'm trying to use ggvis to make a plot similar to this one
and I'm running into two issues.
First, I've tried assigning the line stroke color as the legend shape but ggvis always keeps the circles. In addition, it doesn't recognize the dashed lines either.
library(ggvis)
data <-data.frame(region=rep(c("A","B","C"),5),c=rep(seq(1980,2000,5),3), val=rnorm(15))
data %>%
group_by(region) %>%
ggvis(~c, ~val) %>%
layer_smooths(stroke=~region, strokeDash = ~region,strokeWidth := 3, strokeOpacity := 0.65) %>%
add_axis("y", title="y") %>%
add_axis("x", title="y", format=####) %>%
add_legend(c("stroke","strokeDash")) ## Adding this does not update the legend to recognize the line color or dashes.
Some asked something here but no one answered.
Finally, I'd like to place the legend names of each region next to the lines just as in the first graph. For this, I haven't found out how to even start.
Any help is appreciated.
UPDATE:
I asked how to have x axis labels as numeric and the answer was adding format = "####" to add_axis.
I didn't manage to find what I needed specifically, but I found a partial solution. See the comments in the code.
data <-data.frame(region=rep(c("A","B","C"),5),c=rep(seq(1980,2000,5),3), val=rnorm(15))
data$region2 <- data$region ## create an additional region variable
data$region2 <- as.character(data$region2)
data$region2[data$c != 2000] <- "" ## Fill every year which is not the last year to be an empty character vector
data %>%
group_by(region) %>%
ggvis(~c, ~val) %>%
layer_smooths(stroke=~region, strokeDash = ~region,strokeWidth := 3, strokeOpacity := 0.65) %>%
layer_text(text := ~region2) %>% ## add this new region variable, which will only write over the last year.
add_axis("y", title="y") %>%
add_axis("x", title="y", format="####") %>%
hide_legend(c("stroke","strokeDash"))

Resources