I want to put a specific line for each bar likes the following:
But, I can't. To do this, I have tried the following code to put a particular text at least, but it does not work anymore:
mydata <- data.frame(A=runif(1:10),
B=runif(1:10),
C=runif(1:10))
highchart() %>%
hc_chart(type = "column", inverted = TRUE) %>%
hc_title(text = "MyGraph") %>%
hc_yAxis(title = list(text = "Weights")) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal",
enableMouseTracking = FALSE)
) %>%
hc_legend(layout="vertical") %>%
hc_tooltip(formatter = function(){ return("<b> test</b><br/>")},
useHtml = TRUE) %>%
hc_series(list(name="A",data=mydata$A),
list(name="B",data=mydata$B),
list(name="C",data=mydata$C))
My question is how can I add red lines into the bar chart for each bar line?
Here is a possible solution:
set.seed(1)
mydata <- data.frame(A=runif(1:10), B=runif(1:10), C=runif(1:10))
library(highcharter)
hc <- highchart() %>%
hc_chart(type = "column", inverted = TRUE) %>%
hc_title(text = "MyGraph") %>%
hc_yAxis(title = list(text = "Weights")) %>%
hc_plotOptions(column = list(
dataLabels = list(enabled = FALSE),
stacking = "normal", groupPadding=0,
enableMouseTracking = FALSE)
) %>%
hc_legend(layout="vertical") %>%
hc_tooltip(formatter = function(){ return("<b> test</b><br/>")},
useHtml = TRUE) %>%
hc_series(list(name="A",data=mydata$A),
list(name="B",data=mydata$B),
list(name="C",data=mydata$C))
# x position of red lines
linepos <- c(1.3, 0.7, 1.8, 1.2, 1.0, 1.6, 0.7, 1.7, 0.8, 1.1)
# height of red lines
lw <- 0.35
for (k in 1:length(linepos)) {
df <- data.frame(x=c(k-1-lw,k-1+lw),y=rep(linepos[k],2))
hc <- hc %>%
hc_add_series(data = df, type = 'line', marker=list(enabled=FALSE),
x = ~x, y= ~y, color='red', lineWidth=5, showInLegend=FALSE,
enableMouseTracking = FALSE)
}
hc
Related
I have made twoplots using plotly, which are working fine individually, but when combined using subplot I can't seem to figure out how to combine the legends. I have tried to use showlegend = F in plot_ly in one of the plots, but this just removes it completely - what I want is to control both subplots with the same legend.
My code is as follows:
coronavirus_not_china <- coronavirus %>%
filter(!(country == "China"))
cases_not_china_plot <- coronavirus_not_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = F) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases outside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = T))
coronavirus_china <- coronavirus %>%
filter((country == "China"))
cases_china_plot <- coronavirus_china %>%
group_by(type, date) %>%
summarise(total_cases = sum(cases)) %>%
pivot_wider(names_from = type, values_from = total_cases) %>%
arrange(date) %>%
mutate(active = confirmed - death - recovered) %>%
mutate(active_total = cumsum(active),
recovered_total = cumsum(recovered),
death_total = cumsum(death)) %>%
plot_ly(x = ~ date,
y = ~ active_total,
name = 'Active',
fillcolor = '#1f77b4',
type = 'scatter',
mode = 'none',
stackgroup = 'one',
showlegend = T) %>%
add_trace(y = ~ death_total,
name = "Death",
fillcolor = '#E41317') %>%
add_trace(y = ~recovered_total,
name = 'Recovered',
fillcolor = 'forestgreen') %>%
layout(title = "Distribution of Covid19 Cases inside China",
legend = list(x = 0.1, y = 0.9),
yaxis = list(title = "Number of Cases", showgrid = F))
And I create the subplots as:
subplot(cases_not_china_plot, cases_china_plot, nrows = 2, margin = 0.05, shareX = T) %>%
layout(title="Coronavirus cases outside China and in China", ylab("Number of cases"))
I am quite new to R, so if there is a smarter way to do what I desire, please let me know.
With the above code, my output is:
I want to recreate a choropleth map with country polygons like the county level choropleth map here. And here is its result.
I try to repeat the same procedure for a world map
library(ggplot2)
library(dplyr)
library(maps)
WorldData <- map_data('world') %>% filter(region != "Antarctica") %>% fortify
Country<-c("United States","Canada","France","Italy","Turkey","United States","Canada","France","Italy","Turkey")
Val<-c(50,67,89,567,9,50,67,89,567,9)
Name<-c('AD',"FGH","BGH","FGFG","EWRW",'ADy',"FGyH","BGyH","FGFyG","EyWRW")
Test<-data.frame(Country,Val,Name)
V1 <- aggregate(Val~Country,Test,sum)
colnames(WorldData)[5]<-"Country"
m2 <- data.frame(merge(V1,WorldData , by=c("Country"), all.x=T))
p <- m2 %>%
group_by(group) %>%
plot_mapbox(x = ~long, y = ~lat, color = ~Val1, colors = c('#ffeda0','#f03b20'),
text = ~Country, hoverinfo = 'text', showlegend = FALSE) %>%
add_polygons(
line = list(width = 0.4)
) %>%
add_polygons(fillcolor = ~Val1,
line = list(color = 'black', width = 0.5),
showlegend = FALSE, hoverinfo = 'none'
) %>%
layout(
xaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE),
yaxis = list(title = "", showgrid = FALSE, showticklabels = FALSE),
mapbox = list(
style = 'light',
zoom = 4,
center = list(lat = ~median(lat), lon = ~median(long))),
margin = list(l = 0, r = 0, b = 0, t = 0, pad = 0)
)
p
but I get:
I think you just need to order your data per the 'order' column in your data:
p <- m2 %>%
arrange(order) %>%
group_by(group) %>%
...
...
That worked for me.
I'm trying to make in R (Shiny) a reversed stacked bar highchart. I already found how to make the graph, but I can't find out how to make the labels on the x-axis positive, like here: https://www.highcharts.com/demo/bar-negative-stack
I've tried to apply the abs() function, but it didn't work so far. Does anyone have a solution?
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Example") %>%
hc_yAxis(title = list(text = ""), labels = list(format = "{value}")) %>%
hc_plotOptions(series=list(stacking='normal'),
column = list( dataLabels = list(enabled = FALSE),
enableMouseTracking = TRUE)) %>%
hc_legend(enabled = FALSE) %>%
hc_xAxis(reversed=FALSE, opposite=TRUE, reversed=FALSE) %>%
hc_add_series(name="neutral", id='neutral', color=c("#766A62"), data=list(2, 8)) %>%
hc_add_series(name="Neutral",linkedTo='neutral',color=c("#ffeeff"),data=list(-5, -3))
I want the values of the bars and the labels on the x-axis all to be positive. Any ideas welcome.
Using your code:
highchart() %>%
hc_chart(type = "bar") %>%
hc_title(text = "Example") %>%
hc_yAxis(title = list(text = ""),labels = list(format = "{value}")) %>%
hc_plotOptions(series=list(stacking='normal'),column = list( dataLabels = list(enabled = FALSE),
enableMouseTracking = TRUE)) %>%
hc_legend(enabled = FALSE) %>%
hc_xAxis(list(categories = c("0-4", "5-9"),
reversed=FALSE ),
list(reversed=FALSE,opposite=TRUE,
reversed=FALSE,
categories =c("0-4", "5-9"),
linkedTo = 0)) %>%
hc_yAxis(
labels = list(
formatter = JS("function(){ return Math.abs(this.value) + '%'; }"))) %>%
hc_add_series(name="neutral",id='neutral',color=c("#766A62"),data=list(2, 8)) %>%
hc_add_series(name="Neutral",linkedTo='neutral',color=c("#ffeeff"),data=list(-5, -3))
Following lines are modified:
hc_xAxis(list(categories = c("0-4", "5-9"),
reversed=FALSE ),
list(reversed=FALSE,opposite=TRUE,
reversed=FALSE,
categories =c("0-4", "5-9"),
linkedTo = 0)) %>%
hc_yAxis(
labels = list(
formatter = JS("function(){ return Math.abs(this.value) + '%'; }")))
Result:
I would like to create a variwide chart in Highcharter to use in R,but the result is empty。
library(data.table)
library(highcharter)
labor_cost <- data.table(country =c('挪威','丹麦','比利时','瑞典','法国','荷兰','芬兰','德国','奥地利','爱尔兰','意大利','英国','西班牙','希腊','葡萄牙','捷克共和国','波兰','罗马尼亚'),cost=c(50.2,42,39.2,38,35.6,34.3,33.2,33,32.7,30.4,27.8,26.7,21.3,14.2,13.7,10.2,8.6,5.5),gdp=c(335504,277339,421611,462057,2228857,702641,215615,3144050,349344,275567,1672438,2366911,1113851,175887,184933,176564,424269,169578))
highchart() %>%
hc_title(text = "欧洲各国人工成本", align="center")%>%
hc_xAxis(type = "category",title = list(text = "* 柱子宽度与 GDP 成正比")) %>%
hc_legend(enabled = FALSE) %>%
hc_add_series(data = labor_cost,type = "variwide",hcaes(x = country,width= gdp),dataLabels = list(enabled = TRUE,format="€{point.y:.0f}"),colorByPoint = TRUE)
The code executes normally,no errors appear.
I'd like to plot a large scatterplot using the highcharter package, but only allow mouse over on a few outliers. Is there a way to enable mouseTracking on one series but not the other?
df <- data.frame( x = rnorm(1000), y = rnorm(1000) )
df$sig <- ifelse( abs(df$x) > 2, "signif", "not")
library(highcharter)
hc <- highchart() %>%
hc_add_series_df(df, type = "scatter", group=sig)
Right now I can only disable mouse over on all points, but the hc_plotOptions says something about using a series array?
hc_plotOptions(hc, scatter = list( enableMouseTracking= FALSE ))
There are a lot of way to do what you want.
I think the simplest is use:
hchart(df, "scatter", hcaes(x, y, group = sig), enableMouseTracking = c(FALSE, TRUE))
(Note this is the development version of highcharter.)
Which is same as:
highchart() %>%
hc_add_series(data = df %>% filter(sig == "not"), type = "scatter", enableMouseTracking = FALSE) %>%
hc_add_series(data = df %>% filter(sig == "signif"), type = "scatter", enableMouseTracking = TRUE)
Or
highchart() %>%
hc_add_series(data = list_parse(df %>% filter(sig == "not")), type = "scatter", enableMouseTracking = FALSE) %>%
hc_add_series(data = list_parse(df %>% filter(sig == "signif")), type = "scatter", enableMouseTracking = TRUE)