I am having a problem with skinny bars in a stacked column chart in highcharter in R. I have created a repo of the code below and am wondering if someone could help me avoid the issue of having the bars get super skinny when I add more than 49 series to the graph.
Any ideas or work arounds would be greatly appreciated.
As always, thank you in advance.
Best,
Nate
library(highcharter)
library(magrittr)
library(viridisLite)
dfmtx<- as.data.frame.matrix(matrix(data = abs(rnorm(n=20*50, mean = 0, sd=1)), ncol = 50))
dfmtx<- dfmtx/rowSums(dfmtx)
df<- data.frame(date=seq.Date(from = as.Date("2001-01-01"), to = Sys.Date(), by="years")[1:20],
dfmtx, stringsAsFactors = F)
hc<- highcharter::highchart() %>%
highcharter::hc_chart(type = "column") %>%
#highcharter::hc_plotOptions(column=list(pointWidth=45, pointPadding=0, groupPadding=0.1, padding=0)) %>%
highcharter::hc_plotOptions(column = list(stacking = "normal"), series=list(cropThreshold=200)) %>%
highcharter::hc_xAxis(categories = df$date, title=list(text="Fake Date")) %>%
highcharter::hc_title(text=paste0("Bars Get Skinny When You Add 50"))
for(i in 2:50){ # Smiles...This Works!
#for(i in 2:51){ # Tears..skinny bars :(
the_name<- colnames(df)[i]
hc<- hc %>%
highcharter::hc_add_series(name=the_name,
data = df[,i],
stack = "SameStack")
}
# Pretty colors...why not?
cols<- viridisLite::viridis(n=length(hc$x$hc_opts$series))
cols<- base::substr(cols, 0,7)
hc<- hc %>%
highcharter::hc_yAxis(title=list(text="Proportion"), max=1) %>%
highcharter::hc_colors(cols) %>%
highcharter::hc_legend(align="center")
hc
You can set the width and height of the chart using hc_size().
for(i in 2:51){ # Tears..skinny bars :(
the_name<- colnames(df)[i]
hc<- hc %>%
highcharter::hc_add_series(name=the_name,
data = df[,i],
stack = "SameStack")
}
# Pretty colors...why not?
cols<- viridisLite::viridis(n=length(hc$x$hc_opts$series))
cols<- base::substr(cols, 0,7)
hc<- hc %>%
highcharter::hc_yAxis(title=list(text="Proportion"), max=1) %>%
highcharter::hc_colors(cols) %>%
highcharter::hc_legend(align="center") %>%
highcharter::hc_size(height = 800) #Setting chart height to 800.
hc
Related
This is a similar question but not quite the same since I would like to do this with dendextend. My issue is that some of the names of my labels are quite long and I would like to just remove them. I had tried a few techniques already.
I tried changing cex = 0
I tried changing the color to white but all this does is hide the labels and when the names are long is still an issue.
I tried plotting labels =F , but this does not work as well. Is there anyway to completely eliminate the labels? Here is an example code.
dend <- USArrests[1:5, ] %>%
dist() %>%
hclust() %>%
as.dendrogram()
dend = dend%>% set("labels_cex", 0) %>% set("labels_col", "white") # change to white however this does not work well because the color bars would just get pushed out
dend = dend%>% set("labels_cex", 0) %>% set("labels_col", "black") # setting cex to 0 does nothing
plot(dend, labels=FALSE ) # labels =F are ignore
colored_bars(colors = cbind (
state= "red" ))
You can use dendrapply to change or remove the label attribute of the leaf nodes:
suppressPackageStartupMessages(invisible(
lapply(c("dendextend", "dplyr"),
require, character.only = TRUE)))
dend <- USArrests[1:5, ] %>%
dist() %>%
hclust() %>%
as.dendrogram()
noLabel <- function(x) {
if (stats::is.leaf(x)) {
attr(x, "label") <- NULL }
return(x)
}
plot(stats::dendrapply(dend, noLabel))
colored_bars(colors = cbind (state= "red" ))
Created on 2020-08-05 by the reprex package (v0.3.0)
Edit
As alternatives, you can always truncate the rownames and/or make some space in the plot and shift the bar down:
suppressPackageStartupMessages(invisible(
lapply(c("dendextend", "dplyr", "stringr"),
require, character.only = TRUE)))
oldpar <- par()
par(mar=c(8,4,4,2))
dend <- data.frame(USArrests[1:5, ],
row.names = str_trunc(rownames(USArrests[1:5, ]), 8, ellipsis="..")) %>%
dist() %>%
hclust() %>%
as.dendrogram() %>% plot()
colored_bars(colors = cbind (state= "red" ), y_shift = -60)
par(mar=oldpar$mar)
Created on 2020-08-05 by the reprex package (v0.3.0)
I asked a similar question a few days ago and have made some progress by talking with some people, but I need a little more help to get this to the point I wanted. So here's where I am:
I want to have a data table of a certain size with columns of data (different scales) and be able to apply a color range (kind of like a heat map) to the values in the cells of the data table depending on how large or small the values in the columns are.
Here is some code that was provided to me:
library(DT)
testrun <- round(runif(100), 6)
data <- data.frame(testrun = testrun)
brks <- quantile(data$testrun, probs = seq(.05, .95, .01), na.rm = TRUE)
clrs_df <- colorRamp(c("white","blue"))(c(0,brks)) %>%
as_tibble(.name_repair ="minimal") %>%
setNames(nm=c("r","g","b")) %>%
mutate_all(~as.character(round(.,digits=0))) %>%
mutate(mycolor=paste0("rgb(",paste(r,g,b,sep = ","),")"))
clrs <- pull(clrs_df,mycolor)
DT::datatable(data,rownames=TRUE,options = list(lengthChange = FALSE, dom='t')) %>%
formatStyle(colnames(data), backgroundColor = styleInterval(brks, clrs))
The above example works for one column of data, but I intend to have multiple columns of data for which I want these color gradients to apply to each column individually.
I've tried substituting mtcars for testrun in the assignment portion of the code and in the brks assignment line, but it just leaves me with a table with no colors applied.
Can someone help point me in the right direction? Any help would be appreciated!
Thank you!
I think you want:
library(DT)
data <- data.frame(
C1 = rnorm(100),
C2 = rgamma(100, 10, 10)
)
dtable <- datatable(data, rownames=TRUE, options = list(lengthChange = FALSE, dom='t'))
colRamp <- colorRamp(c("white","blue"))
for(column in names(data)){
x <- na.omit(data[[column]])
brks <- quantile(x, probs = seq(.05, .95, .01))
RGB <- colRamp(c(0, (brks-min(x))/(max(x)-min(x))))
clrs <- apply(RGB, 1, function(rgb){
sprintf("rgb(%s)", toString(round(rgb,0)))
})
dtable <- dtable %>%
formatStyle(column, backgroundColor = styleInterval(brks, clrs))
}
dtable
I wanted to see an exact output of a Highcharter plot side by side in RStudio Viewer if it possible, exactly showed in this reference: http://jkunst.com/highcharter/highcharts.html, So let me define it like this for a simple usage
highcharter_all_plot <- function(){
library(highcharter)
library(dplyr)
library(stringr)
library(purrr)
n <- 5
set.seed(123)
colors <- c("#d35400", "#2980b9", "#2ecc71", "#f1c40f", "#2c3e50", "#7f8c8d")
colors2 <- c("#000004", "#3B0F70", "#8C2981", "#DE4968", "#FE9F6D", "#FCFDBF")
df <- data.frame(x = seq_len(n) - 1) %>%
mutate(
y = 10 + x + 10 * sin(x),
y = round(y, 1),
z = (x*y) - median(x*y),
e = 10 * abs(rnorm(length(x))) + 2,
e = round(e, 1),
low = y - e,
high = y + e,
value = y,
name = sample(fruit[str_length(fruit) <= 5], size = n),
color = rep(colors, length.out = n),
segmentColor = rep(colors2, length.out = n)
)
print(head(df))
create_hc <- function(t) {
dont_rm_high_and_low <- c("arearange", "areasplinerange",
"columnrange", "errorbar")
is_polar <- str_detect(t, "polar")
t <- str_replace(t, "polar", "")
if(!t %in% dont_rm_high_and_low){
df <- df %>% dplyr::select(-e, -low, -high)
}
highchart() %>%
hc_title(text = paste(ifelse(is_polar, "polar ", ""), t),
style = list(fontSize = "15px")) %>%
hc_chart(type = t,
polar = is_polar) %>%
hc_xAxis(categories = df$name) %>%
hc_add_series(df, name = "Fruit Consumption", showInLegend = FALSE)
}
hcs <- c("line", "spline", "area", "areaspline",
"column", "bar", "waterfall" , "funnel", "pyramid",
"pie" , "treemap", "scatter", "bubble",
"arearange", "areasplinerange", "columnrange", "errorbar",
"polygon", "polarline", "polarcolumn", "polarcolumnrange",
"coloredarea", "coloredline") %>% map(create_hc)
return(hcs)
}
x <- highcharter_all_plot()
#Then plot can be accessed in by calling x[[1]], x[[2]], x[[3]]..
As far as my understanding of side by side plot, I only know of 2 these handy methods, which is:
1) Using par(mfrow)
par(mfrow=c(3,4)) -> (which only can by applied to base plot)
2) Using grid.arrange from gridExtra
library(gridExtra)
grid.arrange(x[[1]], x[[2]], x[[3]], x[[4]], nrow=2, ncol=2)
-> (Cannot work since x not a ggplot type)
So I wanted to know if there is a way that this can be applied? I am new using Highcharter
If you inspect the Highcharter website you provided, you will see that those charts are not sided by side using R, but they are just renderer in separate HTML containers and positioned by bootstrap (CSS). So, if you want to render your charts in an HTML environment, I suggest rendering every chart into a separate div.
But maybe Shiny is a tool you are looking for. Maybe this is a duplicate of Shiny rcharts multiple chart output
Maybe this will help you too: https://github.com/jbkunst/highcharter/issues/37
This is an assignment that I have to boxplot() but I somehow got the data squeezed. I'm new to R :(
I guess the problem is because the x axis labels are too long and not placed vertically, so I've tried and failed (based on this Inserting labels in box plot in R on a 45 degree angle?)
examples <- read.csv("mov.development.csv", sep="\t")
library(dplyr)
movies_rated_67_times <- examples %>%
group_by(movie) %>%
summarize(count=n(), avg_rating=mean(rating))%>%
filter(count == 67)
boxplot_data <- examples %>%
filter(movie %in% movies_rated_67_times$movie) %>%
select(title, rating)
boxplot(rating~title,
data=boxplot_data,
xlab="Title",
ylab="Rating",
xaxt = "n"
)
text(seq_along(boxplot_data$title), par("usr")[3] - 0.5, labels = names(boxplot_data$title), srt = 90, adj = 1, xpd = TRUE);
I want to have a plot like this
But I got this
But with a different type of labels that are not too long, normal code would work
Normal code:
examples <- read.csv("mov.development.csv", sep="\t")
library(dplyr)
movies_rated_67_times <- examples %>%
group_by(movie) %>%
summarize(count=n(), avg_rating=mean(rating))%>%
filter(count == 67)
boxplot_data <- examples %>%
filter(movie %in% movies_rated_67_times$movie) %>%
select(movie, rating)
boxplot(rating~movie,
data=boxplot_data,
xlab="Title",
ylab="Rating"
)
csv file: https://drive.google.com/file/d/1ODM7qdOVI2Sua7HMHGEfNdYz_R1jhGAD/view?usp=sharing
Transforming your title column from factor to character seems to fix it. Additionally I would insert line breaks into some of the movies names and reduce the text size so it fit's into the plot
boxplot_data <- examples %>%
filter(movie %in% movies_rated_67_times$movie) %>%
mutate(title = as.character(title)) %>%
select(title, rating)
boxplot_data[boxplot_data$title == "Adventures of Robin Hood, The (1938)",]$title <- "Adventures of Robin Hood,\nThe (1938)"
boxplot_data[boxplot_data$title == "Wallace & Gromit: The Best of Aardman Animation (1996)",]$title <- " Wallace & Gromit: The Best of\nAardman Animation (1996)"
boxplot_data[boxplot_data$title == "Bridges of Madison County, The (1995)",]$title <- "Bridges of Madison County,\nThe (1995)"
par(cex.axis = 0.7)
boxplot(rating~title,
data=boxplot_data,
xlab="Title",
ylab="Rating")
I have created a dygraph and want change the y-axis from scientific notation to decimal form.
This is what the code looks like:
df_xts <- xts(df$Var1,order.by=df$Date)
dygraph(A_xts,
main="DF - Var1",group="group1") %>%
dySeries("V1",label="Var1") %>%
dyOptions(stackedGraph = FALSE,colors=c("blue")) %>%
dyRangeSelector()
I'm guessing it would be placed under dyOptions but I'm not sure.
Thanks!
With my sample data it looks like this:
df_xts <- xts(runif(10) * 1e10, order.by = as.POSIXct(x = 1:10, origin = "2015-01-01") )
dygraph(df_xts, main="DF - Var1") %>%
dySeries("V1",label="Var1") %>%
dyOptions(maxNumberWidth = 20, stackedGraph = FALSE,colors=c("blue")) %>%
dyRangeSelector