Making pie chart's contours smoother on Shiny - r

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.

Related

Cicular contour map using Plotly

I am trying to create contour plots of film thicknesses on a wafer using plotly, but would like the outputted plot to be a circle since it is a wafer instead of the default square. Do I have to somehow overlay a circle on to the plot and then exclude anything outside of it after the plot is generated? I prefer using plotly if possible since it looks nice. I've tried using ggplot as well but for some reason my data doesn't work with it since the x and y coordinates are apparently irregularly spaced. I've searched around but have not seen any results at least using R.
Thanks!

ggplotly function modifies line thickness and chart width

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 ?

How to move the legend to outside the plotting area in Plots.jl (GR)?

I have the following plot where part of the data is being obscured by the legend:
using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))
I can see that using the "legend" attribute, the legend can be moved to various locations within the plotting area, for example:
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
Is there any way of moving the plot legend completely outside the plotting area, for example to the right of the plot or below it? For these kinds of stacked bar plots there's really no good place for the legend inside the plot area. The only solution I've been able to come up with so far is to make some "fake" empty rows in the input data matrix to make space with some zeros, but that seems kind of hacky and will require some fiddling to get the right number of extra rows each time the plot is made:
groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)
I can see that at there was some kind of a solution proposed for pyplot, does anyone know of a similar solution for the GR backend? Another solution I could imagine - is there a way to save the legend itself to a different file so I can then put them back together in Inkscape?
This is now easily enabled with Plots.jl:
Example:
plot(rand(10), legend = :outertopleft)
Using layouts I can create a workaround making a fake plot with legend only.
using Plots
gr()
l = #layout [a{0.001h}; b c{0.13w}]
values = rand(1:100,(10,10))
p1 = groupedbar(values,bar_position=:stack, legend=:none)
p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
p0=plot(title="Title",grid=false, showaxis=false)
plot(p0,p1,p2,layout=l)

ggplotly created object does not dynamically adjust gridlines when zooming in

I'm trying to understand why plotly objects which are converted from ggplot2 objects using the ggplotly function do not dynamically rescale the grid when using plotly's zoom feature on the html widget.
I would like to find a solution to this without the need to recode all plots as native plotly objects.
Am I missing something, or could it be that this functionality is just not supported yet in the plotly package?
Here is some sample code illustrating the problem (before executing, you need to set the your.path variable to the directory you want the htmlwidget to be saved in):
library(ggplot2)
library(plotly)
library(htmlwidgets)
ggp <- ggplot(data=mtcars, aes(x=hp, y=mpg)) + geom_point() + theme_bw()
ply.ggp <- ggplotly(ggp)
saveWidget(ply.ggp, paste(your.path, "mtcarsGGPLOT.html", sep=""))
On this html widget when you zoom in, the grid scale remains static (e.g. the x grid lines on 100, 200, 300 are the only one you can see, even if you zoom in. There are no new grid lines visible e.g. at 25, 50, 75, etc.)
Compare this to the widget when creating the plot directly in plotly with this sample code:
ply <- plot_ly(data=mtcars, x=mtcars$hp, y=mtcars$mpg)
saveWidget(ply, paste(your.path, "mtcarsNATIVE.html", sep=""))
Here when you zoom in a region of the plot, a new dynamic grid is visible.
EDIT: Could it be that the observation occurs due to the htmlwidgets package rather than plotly or ggplot2. But then again, why would the native plotly graphic render correctly when using htmlwidgets while the ggplot converted one doesn't..?
I've also spent time trying to figure this out and it's actually quite simple.
Set dynamicTicks equal to TRUE within ggplotly.
ply.ggp <- ggplotly(ggp, dynamicTicks = TRUE)
I just tried creating the widget and it worked for me.

Aesthetics for graphs in R WITHOUT using ggplot

I looked for the easthetics to make the histogram look better in my R code, nonetheless I was wondering if I could change the size of the bins or maybe the margin or put a color background in the plot WITHOUT using ggplot.
Thank you.

Resources