How to add a ruler for both x and y-axis? - r

How can I add a ruler to a ggplot plot? I need to see how many mm or cm there is in the plot for the different bars in different scenarios. I want to know how big my output needs to be depending on the number of bars and a great help would be to display mm or cm information for the x and y axis.

Related

How to put two y-axis with different scale on the same side of the plot with ggplot?

I have three variables (Precipitation, Temperature and PAR Radiation) with different scales. I'm trying to plot these three variables together and I put the daily sum of precipitation represented by a barplot on the left side y axis and the daily average of temperature on the right side y axis. I'd like to put another y axis on the right side, with another scale, in order to represent the daily average of PAR radiation, but I can't. I'm using the ggplot package, because it is useful for other reasons.
I'm trying to reach something similar as in the pic:
A discussion of this topic can be found here:
ggplot with 2 y axes on each side and different scales
A workaround solution can be found here:
https://rpubs.com/MarkusLoew/226759
You can work with the Sandard R package
http://evolvingspaces.blogspot.com/2011/05/multiple-y-axis-in-r-plot.html

dygraphs configuring dual x axes

I have a dygraph that is plotting two sets of data. The one x-axis is 1 to 600 and the second y-axis is 1-300. How can both be plotted showing the x-axis and the 300 point plot scaled correctly. See the attached example of what I am getting. Would love to have numbers on the second y axis too.
thanksCurrent Code Example

How can I draw datapoints on boxplot with different color using plotly?

I was trying to draw a boxplot with all the points on it. I need to use multiple colors to show multiple dimensions.
Three boxes with three colors (say, January, February, March in X axis)
Some values in Y
All the points in different (Say, different temperatures - 10 steps in 10) colors << This is the challenge I am confused with.
In the legend only color-point should appear << Also this.
I could do it in ggplot. Tried to convert ggplot to plotly like this,
qq <- ggplotly(gg). It does not work, legends just break.
Could you please drop some light on it? Thank you.

How to represent datapoints that are out of scale in R

I am trying to plot a set of data in R
x <- c(1,4,5,3,2,25)
my Y scale is fixed at 20 so that the last datapoint would effectively not be visible on the plot if i execute the following code
plot(x, ylim=c(0,20), type='l')
i wanted to show the range of the outlying datapoint by showing a smaller box above the plot, with an independent Y scale, representing only this last datapoint.
is there any package or way to approach this problem?
You may try axis.break (plotrix package) http://rss.acs.unt.edu/Rdoc/library/plotrix/html/axis.break.html, with which you can define the axis to break, the style, size and color of the break marker.
The potential disadvantage of this approach is that the trend perception might be fooled. Good luck!

Axis-labeling in R histogram and density plots; multiple overlays of density plots

I have two related problems.
Problem 1: I'm currently using the code below to generate a histogram overlayed with a density plot:
hist(x,prob=T,col="gray")
axis(side=1, at=seq(0,100, 20), labels=seq(0,100,20))
lines(density(x))
I've pasted the data (i.e. x above) here.
I have two issues with the code as it stands:
the last tick and label (100) of the x-axis does not appear on the histogram/plot. How can I put these on?
I'd like the y-axis to be of count or frequency rather than density, but I'd like to retain the density plot as an overlay on the histogram. How can I do this?
Problem 2: using a similar solution to problem 1, I now want to overlay three density plots (not histograms), again with frequency on the y-axis instead of density. The three data sets are at:
http://pastebin.com/z5X7yTLS
http://pastebin.com/Qg8mHg6D
http://pastebin.com/aqfC42fL
Here's your first 2 questions:
myhist <- hist(x,prob=FALSE,col="gray",xlim=c(0,100))
dens <- density(x)
axis(side=1, at=seq(0,100, 20), labels=seq(0,100,20))
lines(dens$x,dens$y*(1/sum(myhist$density))*length(x))
The histogram has a bin width of 5, which is also equal to 1/sum(myhist$density), whereas the density(x)$x are in small jumps, around .2 in your case (512 even steps). sum(density(x)$y) is some strange number definitely not 1, but that is because it goes in small steps, when divided by the x interval it is approximately 1: sum(density(x)$y)/(1/diff(density(x)$x)[1]) . You don't need to do this later because it's already matched up with its own odd x values. Scale 1) for the bin width of hist() and 2) for the frequency of x length(x), as DWin says. The last axis tick became visible after setting the xlim argument.
To do your problem 2, set up a plot with the correct dimensions (xlim and ylim), with type = "n", then draw 3 lines for the densities, scaled using something similar to the density line above. Think however about whether you want those semi continuous lines to reflect the heights of imaginary bars with bin width 5... You see how that might make the density lines exaggerate the counts at any particular point?
Although this is an aged thread, if anyone catches this. I would only think it is a 'good idea' to forego translating the y density to count scales based on what the user is attempting to do.
There are perfectly good reasons for using frequency as the y value. One idea in particular that comes to mind is that using counts for the y scale value can give an analyst a good idea about where to begin the 'data hunt' for stratifying heterogenous data, if a mixed distribution model cannot soundly or intuitively be applied.
In practice, overlaying a density estimate over the observed histogram can be very useful in data quality checks. For example, in the above, if I were looking at the above graphic as a single source of data with the assumption that it describes "1 thing" and I wish to model this as "1 thing", I have an issue. That is, I have heterogeneous data which may require some level of stratification. The density overlay then becomes a simple visual tool for detecting heterogeneity (apart from using log transformations to smooth between-interval variation), and a direction (locations of the mixed distributions) for stratifying the data.

Resources