HighcharteR facetted item type chart - r

I'm trying to make a highcharter item type plot, but I can't make the groups nor the facets like I need. I've been following the existing examples given here and in the highcart's API documentation, but I haven't been able to adapt them to what I need.
The plot data is here. Using said data; this is what I got using ggplot2 + plotly in the meantime. Can you help me replicate this using highcharter?
library(ggplot2)
library(plotly)
ggplotly(ggplot(plot_data, aes(x, y, size = 3, color = Partido, text = name, label = Partido)) +
geom_point() +
facet_wrap(~`¿Cómo votó?`)
)
Output:

Using list() already converts to an items object, like example from this post.
data = list(
list(from = 'Brazil', to = 'Portugal'),
list(from = 'Brazil', to = 'Spain'),
list(from = 'Poland', to = 'England'))
Probably module modules/item-series.js not exist on Highcharter, you can report request for adding on https://github.com/jbkunst/highcharter/issues.

Related

Smooth line with Plotly in R

I have one data set which contain data in two column about Gross salary and tax wedge.You can see data with code line below:
SAMPLE_WAGES_TAX_WEDGE_TEST<-data.frame(
GROSS=c(10,20,30,40,50,60,70,80,90,100,200,300,400,500,600,700,800,900,1000),
TAXWEDGE=c(30.1,30.4,30.7,30.9,29.1,28.9,28.6,28.5,27.9,27.2,27.1,27.0,27.0,26.8,25,24,23,21,19)
)
So my intention is to plot graph with plotly package and make some smooth line similar like function geom_smooth() from ggplot2.
library(data.table)
library(plotly)
dat <-as.data.table(SAMPLE_WAGES_TAX_WEDGE_TEST)
fig <- plot_ly(dat, x = ~GROSS, y = ~TAXWEDGE, name = "Before reform", type = 'scatter',
mode = 'lines',line = list(dash = "solid") )
I try with this line of code but I can't add smooth line so can anybody help how to solve this problem ?
There are two ways how to solve this issue:
1) Convert ggplot object into plotly
ggfig <- ggplot(dat, aes(x=GROSS, y=TAXWEDGE) ) +
geom_line() + geom_smooth()
ggplotly(ggfig)
2) geom_smooth() is based on the loess smoother. You need to fit the loess first and use it in add_ribbons in connection with plot_ly object. Check this great blog entry (the last example) for implementation guidance on loess and other smoothers.
Referring and complementing the answer in Adding a smoothed line to a plotly chart, you may directly use plotly with the shape = 'spline' option within the line marker specifications in order to get that type of smoothing (see e.g. https://en.wikipedia.org/wiki/Spline_interpolation):
plot_ly(SAMPLE_WAGES_TAX_WEDGE_TEST, x = ~GROSS, y = ~TAXWEDGE, type = 'scatter',
mode = 'lines', line = list(shape = 'spline', smoothing = 1.3))
where the optional parameter smoothing serves to (slightly) tune the degree of smoothing (see https://plotly.com/r/reference/scatter/#scatter-line-smoothing). Equivalently, using the specific add_lines trace function,
plot_ly(SAMPLE_WAGES_TAX_WEDGE_TEST, x = ~GROSS, y = ~TAXWEDGE) %>%
add_lines(line = list(shape = 'spline', smoothing = 1.3))

qichart and plotly making list instead of chart?

I am currently following a tutorial to graph some data using qichart2. When I input the code, it creates a List instead of a graph. The same thing happened to me previously when I was trying to follow a plotly tutorial.
ControlChart <- qic(Force,
data = HudsonData,
chart = 'i',
title = NULL,
xlab = NULL,
x = TimeStamp,
ncol = 1,
show.labels = TRUE,
point.size = 3,
scales = "free_y",
facets = ~ Limb)
This creates a List of 9 instead of a plot like it did in the tutorial.
Is there something I have set up in my R Studio that is creating Lists instead of plots? When I use ggplot2 plots work fine.
Let me know if more info is needed that would be helpful.

How to use a continuous variable as the color argument for a barplot with plotly?

I've been working with a flexdashboard generated using Rmarkdown and had issues with a few plots not displaying. After much testing, I determined that the color = argument I was sending to the following plot was causing the problem:
p1 <- plot_ly(coursetable2,
x = ~title,
color = ~open+enrolled,
y = ~percent,
type = 'bar',
name = 'Seats Taken',
text =~paste('Seats Taken: ', enrolled, '</br> Open Seats: ' , open))
p1
The open and enrolled variables are continuous numeric values.
The plot generated fine within RStudio, but failed to display in the html file generated once I knit the file.
Curiously enough, this also made a leaflet plot within the same file fail to display in the final html file. The leaflet plot code is as follows:
mytext<-paste("City: ", maptable$name, "<br/>", "People: ",
maptable$val, "<br/>", sep="") %>%
lapply(htmltools::HTML)
pal<-colorNumeric(
palette = "viridis",
domain = maptable$val)
leaflet(maptable) %>%
addTiles() %>%
setView( lat=41.5, lng=-73 , zoom=8) %>%
addProviderTiles("OpenStreetMap.BlackAndWhite") %>%
addCircleMarkers(~long, ~lat,
fillOpacity = 0.7, color=~pal(val), radius=~sqrt(val)+5, stroke=FALSE,
label = mytext,
labelOptions = labelOptions( style = list("font-weight" = "normal",
padding = "3px 8px"), textsize = "13px", direction = "auto")
)
The warning I receive says:
textfont.color doesn't (yet) support data arrays
When I take the color argument out of the above code for the p1 plot, all of my graphics display.
If I define a palette, as in the code below, all plots generate fine.
pal2<-colorNumeric(
palette="viridis",
domain=coursetable2$enrolled+coursetable2$open
)
p1<-plot_ly(coursetable2,
x = ~title,
showlegend=FALSE,
color = ~pal2(open+enrolled),
y = ~percent,
type = 'bar',
name = 'Seats Taken',
text =~paste('Seats Taken: ', enrolled, '</br> Open Seats: ' , open))
p1
This solves the color problem I was having, but I'm stumped as to why passing the color argument in the original code fails. And why BOTH plots fail in this case. Is my fix the ideal solution for passing a continuous variable to the color argument for a bar plot?
Thank you in advance!
As a general solution to formatting plot.ly using unsupported data types, try casting the data from your unsupported type (data array) to a supported type (factor) using the base-r function as.factor(). In python you can do the equivalent using astype("category").
If you're using R, you could cast inside the plot.ly object as follows:
p1 <- plot_ly(coursetable2,
x = ~title,
color = as.factor(~open+enrolled),
y = ~percent,
type = 'bar',
name = 'Seats Taken',
text =~paste('Seats Taken: ', enrolled, '</br> Open Seats: ' , open))
p1
You can also use this approach to solve similar errors with other plot.ly formatting constraints; a cursory check and I was able to reproduce both the warning message and solution above for the 'trace' attribute 'line.color'.
Warning: line.color doesn't (yet) support data arrays

Waterfall plot GenVisR

I'm using the function waterfall from GenVisR package. As the vignette suggests, I am changing the top plot with custom values. However, those values cannot be specified and appear in gray as "Undefined" (see image)
.
I would like to add a layer with ggplot in order to define (and colour) the barplot like the following plot:
In the vignette the authors have an argument called mutBurdenLayer that seems to do the job but I'm not able to change the graph.
Any ideas?
Here is my code:
library(GenVisR)
library(ggplot2)
library(RColorBrewer)
custom_pallete <- brewer.pal(8, "Paired")
waterfall(VCFs, coverageSpace = 179977, main_geneLabSize = 5,
mainLabelSize = 2, mainLabelCol="HGVSp", mainDropMut = T,
mainPalette = custom_pallete, mainXlabel = T)
I would like to add something like:
mut_burden_layer <- theme(...)
waterfall(VCFs, coverageSpace = 179977, main_geneLabSize = 5,
mainLabelSize = 2, mainLabelCol="HGVSp", mainDropMut = T,
mainPalette = custom_pallete, mutBurden = mut, mainXlabel = T,
mutBurdenLayer = mut_burden_layer)
Being mutBurden a data table with the new proportions and mutBurdenLayer the parameter I would like to custom.

Can you plot a table onto a ggmap similar to annotation_custom method for non- Cartesian coordinates

I have been playing around with ggplot2 a bunch and found Adding table within the plotting region of a ggplot in r
I was wondering is there any method for this for plotting using non cartesian coordinates, eg if map coordinates were used for the positioning of the table. I had some maps and thought it would be cool if they could have their corresponding data in a table for points to show more detail.
If anyone knows a work around for annotation_custom for non cartesian coordinates it would be greatly appreciated.
EDIT:Here is a image of what my map looks like, I was just thinking is there another way to plot the table on the left side of this.
EDIT: here is what Im attempting to do
EDIT: Here is the basic code structure for the plot
library(ggplot2)
library(ggmap)
plotdata <- read.csv("WellSummary_All_SE_NRM.csv", header = T)
plotdata <- na.omit(plotdata)
plotdata <- plotdata[1:20, c("Unit_No","neg_decimal_lat", "decimal_long", "max_drill_depth", "max_drill_date")]
map.plot<- get_map(location = c(min(plotdata$decimal_long),
min(plotdata$neg_decimal_lat),
max(plotdata$decimal_long),
max(plotdata$neg_decimal_lat)),
maptype ="hybrid",source = "google", zoom=8)
theme_set(theme_bw(base_size = 8))
colormap <- c("darkblue","blue","lightblue", "green", "yellow", "orange","darkorange", "red", "darkred")
myBreaks <- c(0,2, 10, 50, 250, 1250, 2000, 2500)
static.map <- ggmap(map.plot) %+% plotdata +
aes(x = decimal_long,
y = neg_decimal_lat,
z= max_drill_depth)+
stat_summary2d(fun = median, binwidth = c(.03, .03),alpha = 0.7) +
scale_fill_gradientn(name = "depth", colours= colormap, breaks=myBreaks,labels = format(myBreaks),
limits= c(0,2600), space = "Lab") +
labs(x = "Longitude",y = "Latitude")+
geom_text(aes(label=Unit_No),hjust=0, vjust=0,size=2,
position = position_dodge(width=0.9), angle = 45)+
coord_map()
#Creates image of the plot in file to Working Directory
filename=paste("2dmap",".png", sep="")
cat("\t",filename,"file created, saving...\n")
print(static.map)
cat("\tpassed mapping, file now being made\n")
ggsave(filename=filename,
plot = static.map,
scale = 1,
width = 6, height = 4,
dpi = 300)
I will try to upload the data today, cheers for some of the pointers already!
I have uploaded the data, dont worry about the positioning of the gradient values and text tags as I can fix them later I will also link the current ggmap code but I am using a very large loop for the data to be sorted.
https://drive.google.com/file/d/0B8qOIJ-nPp9rM1U1dkEzMUM0Znc/edit?usp=sharing
try this,
library(gridExtra)
grid.arrange(tableGrob(head(iris)), qplot(1,1), ncol=2)
annotation_custom wouldn't help, it's meant for adding things inside the plot panel, not to the side.

Resources