I've been using ggplot to plot my map using shapefile.
The plot will change its size when I resize the window (I'm using Rshiny).
I've tried to fix the height of the plot like:
plotOutput(outputId = "plot", height = "700px", width = "100%")
However, it will lead to distortion of map, and a scroll bar occurs which I don't want it. Is there any way to keep my map plotting without distortion, and can resize when the window size change?
Thank you so much!
Related
I'm trying to add multiple plots from ggplot into my Latex report. Most plots have different dimensions, as some need to be rectangle shaped, where as some need to be square shaped. To shape the plots, I've been changing the height and width parameters in ggsave. However, this also changes the font sizes. How do I have consistent font sizes for all my plots, and also be able to change the width and height of each plot separately?
I've already specified the font size in the plot using theme(text = element_text(size=5)), but it doesn't seem to actually stay consistent between the plots.
I use ggplotly in my shiny app and use the layout function for the title. When I want to pan the picture, the title and images stay on the same spot while the plot is moving.
Is there a way to let them move with the plot? I know the pan function is to look at parts of the plotted values, but I also got info in the images and title. Besides that, I want the plot to be a certain size, even on mobile and than I need the plot to be able to scroll.
MRE:
library(ggplot2)
library(plotly)
plot<- ggplot(iris,aes(Sepal.Length,Sepal.Width)) + geom_point()
ggplotly(
plot,tooltip="text")%>%
layout(title = list(hjust= 0.5,text = "Title"))
Adding the title through ggplot doesn't solve this.
EDIT:
I made a scrollable box, which solves it a little bit. I want to use the whole plot to scroll when there is a finger/cursor clicking and moving.
Otherwise an other solution is just setting the aspect ratio of the plot instead of the absolute size. Anyone know something about that?
I have an R blog post with some leaflet and plotly figures. I can set width = "100%" in the leaflet() function, and the leaflet map will change shape as I resize my browser window (or view on mobile). I've tried the same width = "100%" in plot_ly(), but the plot doesn't resize for smaller windows and requires horizontal scrolling.
Any ideas? I'm using blogdown/hugo if that is helpful.
Use the chunk option:
```{r, out.width='100%'}
Long story short, I make youtube videos for my students and I produce a lot of graphics. Up until now, I have imported the images into premiere, resized them, then placed a separate white background image behind the ggplot graphics. This is a bit tedious and I would love to be able to export directly from R into the appropriate size (1920x1080 p) so I don't have to resize and add a background image.
I was able to resize the graphic, but now it stretches it:
I want it to look like this (I put the box around the image and the canvas to make it clearer where the plot ends and the background canvas begins):
I know I can specify the outside margins (see How can I control the canvas size in ggplot?), but that would require me setting the outside margins per plot; Sometimes I have plots that are wider than they are tall and sometimes I have plots that are taller than they are wide. ggsave seems to respect the aspect ratio and maximizes one dimension or the other.
So how do I set the outside margins while maintaining the correct aspect ratio?
You could set the panel size to a specific value,
library(egg)
library(ggplot2)
library(grid)
p <- ggplot() + labs(x = 'x title', y = 'y title')
ggsave('notset.png', p, width = 6.4, height = 3.6, units = 'in', dpi = 300)
ggsave('set.png', egg::set_panel_size(p, width=unit(4, "in"), height=unit(3, "in")),
width = 6.4, height = 3.6, units = 'in', dpi = 300)
I am looking to understand why the legend on my plot is being altered when I resize the width of the plot, and how I can prevent this. Currently, I am using R.
The code I am using is this:
matplot(Year, AR4.Data[1:30,2:10], type=c("o"), pch=1, col=colorscheme, lwd=2)
legend(1998.5, 35, legend = AR4.Data[1:9,49], col=colorscheme, pch=1, cex=0.8)
Before altering the image width, the plot looks as follows:
After changing the width to 1400px, the output of the image is this:
Is there a way to ensure that the legend size is minimized when the plot width is maximized?