ggplot theme argument for axis arrows suddenly not working - r

I have a custom ggplot theme that I have been using for a while now. This week I started getting an error when making plots using this theme stating that plot.new has not been called yet
I've traced the error to an axis.line() argument which i've used before to turn my axis lines into arrows. I was able to reproduce the error below, but i'm confused why this is happening now. Does anyone know why this could be happening or possibly provide some alternatives to changing axis lines to arrows?
Eg:
library(ggplot2)
dat <- mtcars
ggplot(data = dat) +
geom_point(aes(x = wt, y = mpg)) +
theme(axis.line = element_line(color = "black", arrow = arrow(length = unit(0.3, "lines"), type = "closed")))
EDIT:
loading the graphics library (library(graphics)) fixed the issue temporarily. However knitting the markdown file produces the same error as above. It seems my example is not reproducing the error, however it's persisting on my computer even after re-installing RStudio AND updating all packages.

Related

How to scatterplot in RStudio

I am trying to create a scatterplot in rstudio with my data. I am new to rstudio and having a lot of time understanding. The code I have found says plot().
This is what I used: plot(pa2_wti2$ï..Approving, pa2_wti2$ï..Price)
Even when I tried a single it didn't give me a scatter plot. I have tried ggplot but it says it does not find what I have put as the x.
I have tried:
ggplot(pa2_wti2) +
geom_point(aes(x = Price, y= Approving))
ggplot(pa2_wti2) +
geom_point(aes(x = ï..Price, y= ï..Approving))
Any help is welcomed. Thanks!
Here is some additional information:
wti2 <- read.csv("C:/Users/thomp/OneDrive/Desktop/FQM/Data Project 2/WTI Price Only Take 2.csv")
summary(wti2)
table(wti2)
sd(wti2$ï..Price)
wti2_prices2 = rnorm(wti2$ï..Price, mean=43.92, sd=28.1762)
pa2 <- read.csv("C:/Users/thomp/OneDrive/Desktop/FQM/Data Project 2/PA Approval Only Take 2.csv")
summary(pa2)
table(pa2)
sd(pa2$ï..Approving)
pa2_approve = rnorm(pa2$ï..Approving, mean=50.09, sd=11.46188 )
plot(pa2_wti2$ï..Approving)
plot(plot(pa2_wti2$ï..Approving, pa2_wti2$ï..Price)

Enabling autoscale by default on a ggplotly object

I have converted several ggplot2 objects to plotly via the ggplotly command.
After the plot renders, and I publish to my Shiny server, I need to manually click "Autoscale" on the plot before it will autoscale. I want it to use this behaviour by default. I'm unsure exactly how to get the argument in there, and whether it is possible.
My current code
library(ggplot2)
library(plotly)
plot1 <- ggplot(my_data, aes(x=month, y=views, colour=category)) + geom_line(size = 1)
ggplotly(plot1)
I'm unsure of the argument required, and cannot find documentation on how to exactly achieve this. Is anybody able to assist?
The comments to your post point you in an appropriate direction, but the example there is slightly different because it does not involve calling ggplotly on a ggplot object. As it turns out, you can just pipe the layout argument:
ggplotly(plot1) %>%
layout(xaxis = list(autorange = TRUE), yaxis = list(autorange = TRUE))

Server Error: Invalid plot index and many duplicate plots

I am not sure why, but it seems as if my code is plotting LOTS of plots in RStudio. I am new to R and RStudio so I can't figure out how many is being plotted, but I think there are duplicates, previous plots, and the plots I want, all within the Plots tab in RStudio. Also, when I try to scroll to see which plots are created, I am getting popups
I am expecting 5 plots for each state but it seems as though I am getting a lot more
library(ggplot2)
try(data('midwest', package='ggplot2'))
for (s in unique(midwest$state)) {
state_data = subset(midwest, state == s)
print(
ggplot(state_data, aes(x=county, y=percprof)) +
geom_bar(stat='identity') +
labs(title=paste(s)) +
xlab('Counties') + ylab('Percentage') +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
)
}
Your code runs fine for me. Try restarting R. It may be a server issue, but it's nothing wrong with your code.

ggpairs() correlation values without gridlines

I have this code that generates the plot below.
library(ggplot2)
library(GGally)
data(iris)
ggpairs(data = iris[, 1:4], axisLabels = "none", switch = "both")
I'd like to do three things with this plot: 1) remove the gridlines in the correlation windows; 2) increase font size of the x-y axes labels; and 3) make these label-backgrounds white (instead of gray). The first question was addressed about 4 years ago here and here, and it seems one would need to either rebuild GGally package, or use a custom code from GitHub. Both options are pretty heavy for a newbie like me, and I am wondering if someone has figured out an easier method by now. I have not found my 2nd and 3rd questions addressed anywhere.
Thanks.
The first request can be handled by:
+theme(panel.grid.minor = element_blank(),
panel.grid.major = element_blank())
The size of the axis labels (which are really in "strips" can be handled with this additional arguemnt to theme:
... , strip.text = element_text(size = 5))

Automatically adjust plot title width using ggplot

I am fairly new to R/ggplot2 and still learning on the go. Hopefully I am not missing something obvious!
I am trying to create several different plots using ggplot2 that I am layouting using the function plot_grid from the cowplot package to make the plots visible side by side and add plot numeration and captions. The problem is that if the generated plots are displayed in a small window or I have many plots beside one another then the titles of the two plots sometimes overlap. To solve this problem I tried to automatically insert line breaks in my too long titles using code I found in another thread since I wanted the text size of the titles to stay constant.
Using the following code I can easily automatically insert the necessary line breaks to make my title a specific width, but the problem is that I always need to enter a numeric value for the width. Depending on the number of plots I am inserting this value would of course change. I could of course go through my code and manually set the width for each set of plots until it is the correct value, but I was hoping to automate this process so that the title width is adjusted automatically to match the width of the x-axis. Is there anyway to implement this in R?
#automatically line break and add titles
myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
plot +
labs(title = paste(strwrap(plot.title, width = 50), collapse = "\n"),
x = x.title,
y = y.title)
}
# generate an example plot
data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point()
title <- "This is a title that is very long and does not display nicely"
myplot_theme1(plot1, plot.title = title)
My test plot
I have tried searching but I haven't found any solutions that seem to address what I am looking for. The only solution I did find that looked promising was based on the package gridDebug. This packages doesn't seem to be supported by my operating system anymore though (macOS Sierra Version 10.12.6) since when I try to install it I get the following error message:
Warning in install.packages: dependencies ‘graph’, ‘Rgraphviz’ are not available
And on the CRAN package documentation it states that the package is not even available for macOS El Capitan which was my previous operating system. If someone knows what is causing this issue so that I could try the solution from the above thread that would of course be great as well.
One idea (but perhaps not an ideal solution) is to adjust the size of text based on the number of characters in the title. You can adjust ggplot properties using theme and in this case you want to adjust plot.title (the theme property, not your variable). plot.title has elements size and horizontal justification hjust, the latter is in range [0,1].
# generate an example plot
data_plot <- data.frame(x = rnorm(1000), y = rnorm (1000))
plot1 <- ggplot(data_plot, aes(x = x, y = y)) + geom_point()
title1 <- "This is a title that is very long and does not display nicely"
title2 <- "I'm an even longer sentence just test me out and see if I display the way you want or you'll be sorry"
myplot_theme1 = function (plot, x.title = NULL, y.title = NULL, plot.title = NULL) {
plot +
labs(title = plot.title,
x = x.title,
y = y.title) +
theme(plot.title = element_text(size=800/nchar(plot.title), hjust=0.5)) # 800 is arbitrarily chosen
}
myplot_theme1(plot1, plot.title = title1)
myplot_theme1(plot1, plot.title = title2)

Resources