I need to draw market profile (aka volume profile) chart in R.
Above is an example of what I want. Horizontal axis is date. On the vertical axis I have level. And I also need to have horizontal bar at every date and every level that shows volume (bar to the right) and count (bar to the left).
My data looks like this. I have a date and level columns that I want to use for groups and volume and count to show as values.
date level volume count
1: 2019-03-04 00:00:00 0.4 50193087 51
2: 2019-03-04 00:00:00 0.1 30030902 50
3: 2019-03-04 00:00:00 -0.3 33674196 53
4: 2019-03-04 00:00:00 0.6 43566324 64
5: 2019-03-04 00:00:00 -0.5 74949678 66
6: 2019-03-04 00:00:00 -0.4 35799917 58
I don't even know where to start with that, it seems that I can't any existing chart types and even a combination. Stacked bar chart isn't going to work, because each bar's width needs to be adjusted according to volume/count. I was thinking about using population pyramid, but I'm not sure if I can use proper x axis (date) and also left bar would be almost invisible because right bar has much larger values and the share the same axis.
Does anybody know how I can draw this chart in r? Preferably using plotly or ggplot2.
UPDATE: My data contains several dates, so chart actually should look like this
And here is new sample of data
date,level,volume,count
2019-03-04,0.4,50193087,51
2019-03-04,0.1,30030902,50
2019-03-04,-0.3,33674196,53
2019-03-04,0.6,43566324,64
2019-03-04,-0.5,74949678,66
2019-03-04,-0.4,35799917,58
2019-03-04,-0.1,99431328,46
2019-03-05,0.8,85373468,45
2019-03-05,0.5,76080717,51
2019-03-05,-0.7,45250685,48
2019-03-05,-0.9,47862662,48
2019-03-05,-0.2,43731758,48
2019-03-05,0.3,43375430,45


Okay this is going to be my best guess at what is asked, though I'm not totally sure.
First I read in your data, which the poster can probably skip but may help others reproduce it:
Then I copy your data to two seperate
data.frames, giving each another facetting variable:And then we build the plot:
And the result:
Is that anywhere near what you had in mind?
EDIT: A solution for multiple dates (sort of) on the x-axis. First I refactored the data to get more dates in there:
Next, it's going to look a lot like the previous plot with an addition of a
geom_blank()that ensures every volume/count has the same x-axis range and using date as a facetting variable.Which looks like this:
You'll notice that the dates aren't particularly well-placed and we can't switch them in our code with the variable otherwise it will be grouped by count/volume instead of by date. Also there is no easy way to de-duplicate the dates. In my defence, mapping 3 vastly different variables to the same axis is a bit overkill. But, if you really want the date labels to look pretty, I suggest you have a look at this question: Nested facets in ggplot2 spanning groups, or edit them outside R with an image editing program.