ggplot2 polar plot axis label location - r

This is just a extension for a old question
ggplot2 polar plot arrows
You will find the x axis is out of the most_out circle.
In ggplot2, I use "panel.grid.major = theme_line(colour = "black", size = 0.2, linetype=2)" to get the dashed circle, just as below:
So my question is how to make the axis label (180, 135, 90, .....) outside of the circle, because the text are merge with the circular lines.
I try to use "hjust" or "vjust" to adjust the distance between text and axis. But it does not work.
So do you have some ideas about this problem?
Thanks first!!!!

You have not provided code to reproduce the problem so this will be just a guess.
I've used whitespace, \n in particular, to move text "away" in the past. Perhaps a custom formatter might work here. Here is how you can write a custom tick mark label formatter.
If this fails, you can always hide the axis labels and paint them yourself using geom_text by adding another layer.
Hope this helps. #hadley's book on ggplot2 is very good, by the way.

I came across this question while I was trying to fix a similar issue myself. One workaround is pretty much covered in the answer to this post: Remove extra space and ring at the edge of a polar plot
You would have to adjust the limits of the x scale to match your axis labels. You would also have to create a new scale bar corresponding to the radial length of your arrows (the 0-300 scale bar on the left side of your plot), since
axis.text = element_blank
takes the scale bar away as well.

Related

How to Draw the Solution Curve in Julia with Arrow Correctly Using Plots?

I am trying to plot the solution curve from this page:
dynamicalsystem
But, I can't draw the arrow in the curve pointing to the origin, and the curve is not spiraling as well. Is something wrong with my code?
using MTH229, ForwardDiff, Plots, LaTeXStrings, SymPy
gr()
t = range(0, stop=21, length=10000)
x = #. exp(-2t)*cos(t)
y = #. exp(-2t)*sin(t)
plot(x, y, arrow = :closed, label=L"x(t)",
xlims=(-0.1,1), ylims=(-0.1,0.21))
You do not see a spiral, because the plot you have linked is not accurate (i.e. it has a wrong scale - most likely for didactic purposes). You can see that you have a spiral, by e.g. plotting the angle of the point plot(t, angle.(x + im * y)). The problem is that the plot absolute value of the points you plot gets very small very fast so these spirals are not visible.
Now the other issue is how to plot the arrow. The arrow in your case is just not visible because it is plotted at the end of your curve. I think (but maybe there are better ways to do it) that the simplest solution to pick a place to add it separately. For example after doing an initial plot add plot!(x[240:241], y[240:241], arrow=:closed) will add an extra arrow in the segment of your curve and the arrow will be visible (you just need to decide on the color of the arrow).

R: How to center the tick labels (putting labels between tick marks) using ggplot?

I was using R ggplot for data visualization, and I met the problem centering the tick labels between tick marks on x/y axes. Here's a similar question but the implementation is matplotlib.
This is what I had (the wrong version):
And this is what I expected:
THANKS A LOT!!!
It is hard to reproduce your plot without the data you used to create the plot or the code you used to generate the plot
You can move the x-axis labels to the right by adding the following code to your plot
+ theme(axis.text.x=element_text(hjust=-0.5))
Additionally, one option might be just to rotate the x-axis labels instead of moving them. You can do this by adding the following code
+ theme(axis.text.x=element_text(angle = 90))

Can I place my ggplot2 legend right beside the horizontal axis label?

In order to save as much as white space as I can, I would like to place the legend entry at the same height as the horizontal axis label. Can this be done and if so, how?
Here's a plot illustrating what I am hoping to achieve, with current and hoped-for legend position illustrated using a (manually added) green box.
I currently place the legend using theme(legend.position=c(0.87,0.1)) (noting that the exact coordinates are not relevant). Ideally, this route would allow for values outside of the [0,1] domain but it appears not to allow for that.
theme(legend.position="bottom") places the legend well outside of the plotting area, thus taking up more white space than I am willing to spare.
You just might have to play around with negative values regarding the y-coordinates of your legend.position-vector.
Here's an example:
library(ggplot2)
ggplot(iris, aes(Sepal.Width, Sepal.Length, color=Species))+
geom_line()+
facet_wrap(~Species)+
theme(legend.position=c(0.87,-0.01))
Note the -0.01 value. Is this what you're looking for?

How do I use ggrepepl to avoid all geoms?

I'm trying to use ggrepel to create text labels for charts that I'm working on using R and ggplot2. I'm finding it very useful for repelling away from a single point, but I often run into a problem where it overlaps some other plot objects.
I'm trying to add it to the plot like so:
plot + ggrepel::geom_text_repel(aes(y = Ratio, label = Ratio), direction = "y")
Is there some way that I can tell ggrepel to avoid everything on the ggplot? I've tried searching and coming up with something for this but I'm stuck.
I hope my question is clear enough, thank you.
ggrepel does not allow avoiding all geoms.
In your case, as a workaround, you can use nudge_y = 0.1 in to shift all labels up.
Often, in such cases, you would want more space for the labels. This can be achieved with e.g. + scale_y_continous(expand = scales::expansion(mult = c(0.05, 0.2)))
ggrepel will not label, but repel from, points with a empty ("") label. So in general, as a workaround, you can try to generate data that would cover the other geoms and include that data with empty labels in your geom_text_repel call.

How to preserve plot sizes when stacking with common x-axes in ggarrange?

I am trying to stack plots with common x- and y-axes in ggplot. What I want to do is have only the bottom plot show the x-axis labels and titles. But I've never been able to figure out how to do this cleanly in ggplot2 without having the bottom plot be squished by carrying the virtue of the x-axis labels/title. There must be an easy way to do this- everyone wants to stack graphs, right?!
I'm currently trying with ggarrange. Example code below. Note that the bottom plot gets compressed vertically because it has the tick and axis labels. I could just have the top two have white font labels/title, but then there is an unseemly amount of margin space between the three if you use that hack.
I'm definitely open to packages other than gpubr, but I am hoping for something not too elaborate that I can use in subsequent situations, as I'm sure I'll encounter this again...
Help, please!! -Ryan
#
require(ggplot2); require(ggpubr)
X=data.frame(seq(as.Date("2001-01-01"),as.Date("2001-12-31"),by='days')); colnames(X)='date'
X$Y1=sample(80:100,size=nrow(X),replace=T)
X$Y2=sample(100:120,size=nrow(X),replace=T)
X$Y3=sample(50:70,size=nrow(X),replace=T)
plot.Y1= ggplot(X, aes(x=date,y=Y1))+
geom_line()+lims(y=c(50,150))+
theme(axis.title.x = element_blank(),axis.text.x=element_blank())
plot.Y2= ggplot(X, aes(x=date,y=Y2))+
geom_line()+lims(y=c(50,150))+
theme(axis.title.x = element_blank(),axis.text.x=element_blank())
plot.Y3= ggplot(X, aes(x=date,y=Y3))+
geom_line()+lims(y=c(50,150))
x11(10,8)
ggarrange(plot.Y1,plot.Y2,plot.Y3,nrow=3,ncol=1)
Bottom plot is squished!
try this,
egg::ggarrange(plot.Y1,plot.Y2,plot.Y3,ncol=1)

Resources