ggplotly function modifies line thickness and chart width - r

I encoutered problems concerning lines thickness and charts width using ggplotly function (from plotly R package )
Here is my ggplot chart that I want to make dynamic using ggplotly
graphe_clustering <- ggplot(data=dtaLngC, aes(x=DAT_RLV, y=vol_cso_norm))+
geom_line(aes(color=contrat),lwd = 0.5)+
facet_wrap(~Cluster, scales = "free")+
theme(legend.position="none")
For now, everything is fine. But when I use ggplotly(graphe_clustering), here is what I get:
There are two problems:
Thickness of lines are too big
Widths of charts located in the middle of my windows strongly decreased
Does someone has any advices concernings those problems ?

Related

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))

ggplot with the same width and height as ggsave(width=x, height=y)

Within R-Studio, I am generating plots with ggplot, then I save them with ggsave() for further use (I know, not ideal, but non-optional right now).
My problem is that when I generate the plot before saving it, R shows it to me in a particular size. With ggsave, I set width and height, so the elements displayed shift etc. I only see this after saving the plot.
I want R to show it to me before. I thus assume that I need to set the size of the plot within ggplot() somewhere , not in ggsave().
How can I do this in the least complicated fashion?
library(ggplot2)
ggplot(mtcars, aes(mpg,disp)) + geom_point() +
labs(title="Rocket science title that will get cut by ggsave")
ggsave("rocketScience.png", width=10, height=7, unit="cm")
You can use the set_panel_size() function from the egg package.
With this function you can fix the panel size of the plot. This can be very useful when creating multiple plots that should have the exact same plotting area but use varying axis labels or something similar that would usually slightly change the panel dimensions. Especially useful for presentations with seamless animations or publications. It also ensures the same dimensions in the preview and the saved plot.
p <- ggplot(mtcars, aes(mpg,disp)) +
geom_point() +
labs(title="Rocket science title that will get cut by ggsave")
#to view the plot
gridExtra::grid.arrange(egg::set_panel_size(p=p, width=unit(5, "cm"), height=unit(7, "cm")))
#to save the plot
ggsave(filename = "myplot.pdf", plot = egg::set_panel_size(p=p, width=unit(5, "cm"), height=unit(7, "cm")))
Within ggsave you can still manipulate the size of the whole "page" saved, but this will only influence the amount of white space around the plot. The actual panel size will stay fixed.
The example plot from above with 5cm or 15cm as width:
I don't believe this is achievable via ggplot settings; you might get around it if using RMarkdown, as you can set with and height of an output of a markdown chunk via fig.width and fig.height params.

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)

ggmosaic plot with asymmetric offset

I have a mosaic plot generated by ggmosaic:
ggplot(data.frame(a1=c(T,T,F,F), a2=c(T,F,T,F), a3=c(1,3,3,3))) +
geom_mosaic(aes(weight=a3, x=product(a1,a2), fill=a1))
I would like to widen the space between the vertical bars without changing the height of the space between the stacked columns:
I've tried using the offset parameter, but it seems to work on both dimensions, and can't isolate just one. An answer using vanilla ggplot is acceptable, but a ggmosaic-only solution is preferred.
A less-than-ideal workaround using using geom_bar:
ggplot(data.frame(a1=c(T,T,F,F), a2=c(T,F,T,F), a3=c(1,3,3,3)), aes(width=c(.4,.6,.4,.6)))+
geom_bar(aes(x=a2, y=-a3, fill=a1), position = "fill", stat = "identity")

Making pie chart's contours smoother on Shiny

I am trying to have a nice looking plot for two (or possibly more) continuous values in the form of a pie chart using ggplot2.
The code is the following :
library("ggplot2")
library("ggthemes") ## for theme_economist
df <- data.frame(origin=c('with','without'),value=c(24536,50456))
pie <- ggplot(df, aes(x = "",y=value, fill = origin))+
geom_bar(width = 1,stat="identity")+
coord_polar(theta = "y",start=pi/3)+
theme_economist()
The pie is relatively fine except that the contours of the pie seems to be a bit irregularly drawn.Of course when you export it as a pdf it looks fine but displayed in RStudio or Shiny it doesn't look nice at all.
Is there a way to change the resolution of the ggplot object directly or to make renderplot() aware that we want to display the image at a high resolution ?
I tried modifying the res argument of the renderPlot() function but it distorts the image and do not get rid of the irregularity of the border.
How to make the contours of the pie smoother on Shiny or RStudio directly ? Thanks.
To improve the resolution of the plot on Shiny you have to set the res argument to its default value of 72. It does not make a lot of sense to me but it worked just fine the resolution went from really bad to really nice !
However increasing its value further can lead to distortion of the image.
It seems that for now you cannot change the resolution of the plotting object itself before exporting it or rendering it into Shiny.

Resources