I am trying the following code but am not getting any hatched bars:
plot_ly() %>%
add_trace(type="funnel",
y = c("level one","level two"),
x = c(500,100),
marker = list(pattern = list(shape = "X")))
I am trying the following code but am not getting any hatched bars:
plot_ly() %>%
add_trace(type="funnel",
y = c("level one","level two"),
x = c(500,100),
marker = list(pattern = list(shape = "X")))
Copyright © 2021 Jogjafile Inc.
I couldn't find a way through Plotly, but I did find a way around it. Essentially, you have two options. You can create and call the pattern definitions. Or you exploit Plotly and repurpose their work. Since I'm all about working smarter, not harder...
I planned on making a funnel with a depth of five layers with a plot pilfered from Plotly's examples. So to create the patterns, I created a 5-bar barplot with patterns. (I actually used the funnel data in the bar plot, but the data is irrelevant.)
The important part is designating the patterns that you want to see in the funnel plot.
I opened the plot in my browser and copied the entire
<g class="patterns">element.Open the plot in the browser. Then right-click on the browser, select 'inspect' or 'developer tools'. (The verbiage depends on your browser.) Go to the elements tab and look for SVG -> defs.
Click on the ellipses on the left side of the screen, then select
copy element. In the next step, I'm going to change the IDs. You may find it a lot easier to change them in HTML before you copy them.If you wanted to change them in HTML (which is probably easier), double-click on the string next to the attribute "id". If something goes wrong, just refresh your browser, no harm done. I made them names that were definitely not keywords in HTML, CSS, or JS, and identified what they represented:
p-slash,p-ex,p-dash,p-plus, andp-dot.Now you just have to take this content and feed it back to Plotly...well, shove it down its throat, really.
This uses the library
htmlwidgets. Since it's only used this one time, I didn't call the library.This call for
onRenderlooks for thedefsof your funnel plot (which will be empty), and replaces it. Then it looks for each layer of the funnel and changes the fill from a color to one of these patterns pilfered from the barplot.One tip...don't forget (or by the way, if you didn't know) Javascript indices start at zero, unlike R which starts everything at 1. So the first layer is layer zero.
When you execute the declarations of
ptnorptn2in R, it will automatically add the "" before each". So call it and then copy the string in the console when you add it to theonRenderfunction in this code.If you have any questions, let me know.