ggplot2 in R -- error message and then also no points being plotted - r

I am working on my data analytics cert (google) on Coursera and there is a hands on activity with ggplot.
It tells me to type this:
ggplot(data = penguins) + geom_point(mapping = aes(x = flipper_length_mm, y = body_mass_g))
into the console, so I did and got this error message:
Error in draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label, :
lazy-load database '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/4.0/gtable/R/gtable.rdb' is corrupt
In addition: Warning messages:
1: Removed 2 rows containing missing values (geom_point).
2: In draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label, :
restarting interrupted promise evaluation
3: In draw_axis(break_positions = guide$key[[aesthetic]], break_labels = guide$key$.label, :
internal error -3 in R_decompress1
When I then put quote marks around the column names, I at least got what looks like a plot, but absolutely no points on the graph. I tried with different col names, because maybe something isn't a number? but that didn't matter, nothing happens with the plot.
Coursera is not really a whole lot of help, and I've been trying to get help from others in the course (since I'm taking it thru an organization) but haven't been able to get any help. Is there someone out there who sees what I'm doing wrong?

Related

Histogram runs into object not found error

I am a student who is quite new to R and am having difficulty linking my dataset to the actual workspace. In particular, I am trying creating a histogram to show what life expectancy looks like across zipcodes of a state, but nothing is showing up.
This is what my code looks like:
install.packages("ggplot2")
library(ggplot2)
ggplot(data = df_mo, aes(x = life_expectancy)) + geom_histogram(color = "tomato")
Here is what my error message in the console states:
>ggplot(data = df_mo, aes(x = life_expectancy)) + geom_histogram(color = "tomato")
Error in FUN(X[[i]], ...) : object 'life_expectancy' not found
>
Here is what my dataset looks like:
This may be quite an elementary problem I imagine but I don't have a clue and have been at this for an hour. I've tried to look this problem up but everything i've seen has some additional bells and whistles added to the code or they are receiving a different error message.
Thank you in advance.
The problem is that the dataframe doesn't have the columns names that you want. As it is shown in the picture the names are V1, V2, .... Something like:
colnames(df_mo) = df_mo[1,]
df_mo = df_mo[-1,]
should do the trick. Also should re-consider the way you are loading the data to R so it uses the first line as column names

ggsurvplot warning"Warning messages: 1: Removed 1 rows containing missing values (geom_text) "

I am using ggsurvplot to create a kaplan-meier curve. I get the following warning message "Warning messages: 1: Removed 1 rows containing missing values (geom_text)." I have seen other posts that are similar and have solved the problem when the image is geom_point.
But the proposed solution [Explain ggplot2 warning: "Removed k rows containing missing values" of adding coord_cartesian does not work with ggsurvplot. I know that I can simply expand my y-axis to remove this warning message but i dont want to do that because then you are unable to differentiate when the curves separate.
My code:
ggsurvplot(survival.primary, ylim=c(0.0,0.20), xlim=c(0,1500), break.x.by=180, censor = F, risk.table = T, fun = "event")
Thank you very much!

Using the QQ Plot functionality in ggplot

I'm brand new to R, and have a data frame with 8 columns that has daily changes in interest rates. I can plot QQ plots for data each of the 8 columns using the following code:
par(mfrow = c(2,4))
for(i in 1:length(column_names)){
qqnorm(deltaIR.df[,i],main = column_names[i], pch = 16, cex = .5)
qqline(deltaIR.df[,i],cex = .5)
}
I'd like now to use the stat_qq function in the ggplot2 package to do this more elegantly, but just can't get my arms around the syntax - I keep getting it wrong. Would someone kindly help me translate the above code to use ggplot and allow me to view my 8 QQ plots on one page with an appropriate header? Trying the obvious
ggplot(deltaIR.df) + stat_qq(sample = columns[i])
gets me only an error message
Warning: Ignoring unknown parameters: sample
Error: stat_qq requires the following missing aesthetics: sample
and adding in the aesthetics
ggplot(deltaIR.df, aes(column_names)) + stat_qq()
is no better. The error message just changes to
Error: Aesthetics must be either length 1 or the same as the data (5271)
In short, nothing I have done so far (even with Google's assistance) has got me closer to a solution. May I ask for guidance?

ggplot "error in rename"

update I have posted my solution below, the culprit was my own rename function that overrode reshape::rename
I have been using the ggplot R package with little trouble until today. Today, I get an error using code that has previously worked, and when I debug it to the minimal working example, it still gives an error;
If I do this:
library(ggplot2)
d<- data.frame(x=1:3,y=1:3)
ggplot(data=d) + geom_line(aes(x,y))
The following error is returned:
Error in rename(x, .base_to_ggplot) :
unused argument(s) (.base_to_ggplot)
The traceback is:
6: rename(x, .base_to_ggplot)
5: rename_aes(aes)
4: aes()
3: structure(list(data = data, layers = list(), scales = Scales$new(),
mapping = mapping, options = list(), coordinates = CoordCartesian$new(),
facet = FacetGrid$new(), plot_env = environment), class = "ggplot")
2: ggplot.data.frame(data = d, aes = c(x, y))
1: ggplot(data = d, aes = c(x, y))
The error does not occur after removing all objects using rm(list=ls()), but it is still not clear to me what object is causing this error or why - how can I figure this out?
Does anyone know what may have gone wrong?
I'm not able to return the same error message that you've posted above. When running your code snippet, I'm getting the following error:
Error: geom_pointrange requires the following missing aesthetics: ymin, ymax
Accordingly, geom_pointrange() is expecting arguments for ymin and ymax. I'll leave it up to you to fill in your pertinent information for what should go into those parameters, but this code executes:
ggplot(data=d) + geom_pointrange(aes(x,y, ymin = y - .5, ymax = y + .5))
The problem is caused because ggplot2 doesn't use namespaces - this will be fixed in the next release.
The error was caused by one of the objects (thanks to pointers from #Chase).
Here is how I debugged and found the culprit. The important part was to use the try() function that keeps the for loop running despite errors
foo <- ls() #get a static list of all suspect objects
for(i in 1:length(foo)) {
print(foo[i])
rm(list=foo[i])
try(ggplot()+geom_point(aes(x=1:2,y=1:2)))
}
This resulted in the following output:
...
[1] "45 reg.model"
Error in rename(x, .base_to_ggplot) :
unused argument(s) (.base_to_ggplot)
[1] "46 reg.parms"
Error in rename(x, .base_to_ggplot) :
unused argument(s) (.base_to_ggplot)
[1] "47 rename"
[1] "48 samples"
...
aha! it was my own function rename that caused the error, since ggplot2 relies on reshape::rename.
Solution: rename the new rename function... how to prevent this in the future? Perhaps study up on the use of namespaces.

Why do I get an error when I run some examples from the online ggplot2 reference manual?

Trying the ggplot2 examples in the online reference manual, and particularly in this page, I fail to produce all but the first of the second example's plots.
> d + stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE)
Error in `[<-.data.frame`(`*tmp*`, var, value = list(`NA` = NULL)) :
missing values are not allowed in subscripted assignments of data frames
In addition: Warning message:
Removed 34912 rows containing missing values (stat_density2d).
I have R ver. 2.10.1 and ggplot2 ver. 0.8.6
What is wrong?
It seems to be the bug reported here:
http://groups.google.com/group/ggplot2/browse_thread/thread/6a7929d2b122efb2

Resources