Thank you for this suppose you got sales data. you got items sold, their quantity and value if I were in excel I would make a bar plot for the value, i.e. sales value, and volume/quantity of items sold as line chart combined with the previous but the latter would use a secondary axis now, I know such a thing is possible by mapping different data to different geoms but I am facing an issue visualising the quantity on a line chart any ideas? x-axis would have categorical labels and the other numeric
It is possible to implement a secondary axis in ggplot2, but there are some problems associated with this type of chart. Hadley Wickham explicitly warned against using them. stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales - not invertible: a given point on the plot scale cannot be uniquely mapped back to a point in the data space - hard to read correctly compared to other options - easily manipulated to mislead: no unique way to specify the relative scales of the axes
If you still want to use a secondary axis (I know it is common in some areas), you need a function to transform from the first y axis to the second. An example: transformfactor = max(data_forPlotting$count) / max(data_forPlotting$mean) Then you can use that in the sec.axis argument inside scale_y_continuous: scale_y_continuous(name = "Count", sec.axis = sec_axis(~ ./transformfactor, name = "Mean"))
@@StatistikinDD thank you for this; if you were to visually communicate the number of items sold and value of sales to each product line, how would you visulaise it? what kind of charts. I am thinking now if I don't implement the secondary axis, I might use the package, patchwork, to patch 2 column charts
@@ahmed007Jaber Yes, I think patchwork is a good choice. I'd use separate charts with clearly labelled y axes and informative titles to minimize the chance of confusion. An alternative to a bar chart is a dot chart which offers a better ink to information ratio.
Great content, thanks so much and keep it coming
Thank you for this
suppose you got sales data.
you got items sold, their quantity and value
if I were in excel I would make a bar plot for the value, i.e. sales value, and volume/quantity of items sold as line chart combined with the previous but the latter would use a secondary axis
now, I know such a thing is possible by mapping different data to different geoms but I am facing an issue visualising the quantity on a line chart
any ideas?
x-axis would have categorical labels and the other numeric
It is possible to implement a secondary axis in ggplot2, but there are some problems associated with this type of chart. Hadley Wickham explicitly warned against using them.
stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales
- not invertible: a given point on the plot scale cannot be uniquely mapped back to a point in the data space
- hard to read correctly compared to other options
- easily manipulated to mislead: no unique way to specify the relative scales of the axes
If you still want to use a secondary axis (I know it is common in some areas), you need a function to transform from the first y axis to the second.
An example:
transformfactor = max(data_forPlotting$count) / max(data_forPlotting$mean)
Then you can use that in the sec.axis argument inside scale_y_continuous:
scale_y_continuous(name = "Count",
sec.axis = sec_axis(~ ./transformfactor, name = "Mean"))
@@StatistikinDD thank you for this; if you were to visually communicate the number of items sold and value of sales to each product line, how would you visulaise it? what kind of charts. I am thinking now if I don't implement the secondary axis, I might use the package, patchwork, to patch 2 column charts
@@ahmed007Jaber Yes, I think patchwork is a good choice. I'd use separate charts with clearly labelled y axes and informative titles to minimize the chance of confusion. An alternative to a bar chart is a dot chart which offers a better ink to information ratio.