Histogram runs into object not found error - r

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

Related

ggplot error in if (node$tag == "span") after trying to split a faceted plot over multiple pages

My dataset includes the variable "annual_TRW" measured over different "survey_year", with 5 "Sample_Core_ID" within each "block" nested within each "site".
The following code was working to plot all samples for a plot subset.
pdf("plots.pdf")
library(ggplot2)
dataset %>%
group_by(site, block, Sample_Core_ID ) %>%
ggplot(., aes(x = survey_year, y = annual_TRW, colour = Sample_Core_ID)) +
geom_line() +
facet_grid(Sample_Core_ID ~ .)
dev.off()
Then, I was trying to split a faceted plot over multiple pages, with each page including the faceted plots (1 column, 5 rows) corresponding to the samples of each block. I was testing using different different functions ggplus::facet_multiple, gridExtra::marrangeGrob, and ggforce::facet_wrap_paginate, but without success i.e. pdf were created empty.
The problem is that, after trying different options, now I get the following error every time that I try to use any ggplot on the same dataset. Now, I dont manage to plot the code that worked before without giving this error.
Error in if (node$tag == "span") { : argument is of length zero
Question 1: How can I solve this error every time I try to use this ggplot?
Question 2: Once this works again, how can I split the faceted plot over multiple pages as explained before?
Thanks a lot in advance!
I tried re-running the entire code, and re-calling the packages, but it doesnt work.
Regarding Problem 1: the error was solved after updating the R version and reinstalling only the necessary packages that worked. The issue might have come from installing the packages ggplus as described here https://github.com/guiastrennec/ggplus

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

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?

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?

File too big when using plotly::api_create and ggplot2::geom_sf functions

I am trying to make online plotting of spatial data using plotly in R but I got an error message "Request Entity Too Large This file is too big! Your current subscription is limited to 524 KB uploads." Any clue about how to solve that? In oder to reproduce my code, you need to (i) register on plotly and (ii) download shapefiles of French departments available on my github repo. The 3 files should be in a folder that is named shapefile. It seems to me that is the ggplot2 function geom_sf that produces files that are too large. My code is below
require(tidyverse)
require(ggplot2)
#Info required for online plotting
Sys.setenv("plotly_username"="replace_by_your_username")
Sys.setenv("plotly_api_key"="replace_by_your_apikey")
#Read shapefile
dep <- sf::st_read("replace_with_the_correctPATH/shapefile/DEPARTEMENT.shp")
#Variable to plot
zz<-runif(length(dep$CODE_DEPT),-10,3)
#ggplot2 object
gg <- dep %>%
mutate(discrete = cut(zz, c(-10, seq(-3, 3, by = 1)))) %>%
ggplot() +
geom_sf(aes(fill = discrete, text = paste("Department:", dep$CODE_DEPT, "<br>", "bli", zz))) +
scale_fill_brewer(palette = "PuOr", name = "bla")
#Plotting the figure on your local computer works
#plotly::ggplotly(gg, tooltip = c("text"))
#Generate an error message
plotly::api_create(gg, tooltip = c("text"),filename = "sthing")
This doesn't appear to be your issue but I experienced the same thing. For future readers, my issue seemed to be that the dataframe I was using (say, 100 rows) was subset from a large dataset (15,000 rows) which was larger than the file limit.
Although my subset was quite small and well within the upload limit, I had to save the subset as a csv, load it back in and use that new loaded dataframe as my plotly upload. Even though the imported dataframe was the same row count as the subset dataframe, I had to break the subset connection to the original larger dataset, don't know why.

error with ggplot2 pie chart

I'm using the following code to create a pie chart with ggplot2, which contains two pie charts next to one another: one for each value of "MotT". Each pie chart need to how the proportions for each "Model". Here is my code:
library(ggplot2)
library(sqldf)
df <- data.frame("MorT" = c(1,2,1,2), "Model" = c(1,1,2,2),
"Values" = c(length(outOfTime1withIns[,1]),
length(outOfMem1withIns[,1]),
length(outOfTime1noIns[,1]),
length(outOfMem1noIns[,1])))
df=sqldf("select Values,
CASE WHEN MorT==1 THEN 'Insuficient Time'
WHEN MorT==2 THEN 'Insuficient Memory'
END MorT,
CASE WHEN Model==1 THEN '1) FSM1 with Insertion Dominance'
WHEN Model==2 THEN '2) FSM1 without Insertion Dominance'
END Model from df")
p = ggplot(data=df,
aes(x=factor(1),
y=Summary,
fill = factor(Model)
)
)
I get the following error after I try to run "df=sqldf("select..."
Error in sqliteExecStatement(con, statement, bind.data) :
RS-DBI driver: (error in statement: near "Values": syntax error)
And of course p is empty. I get
Error: No layers in plot
If I try to call it.
Any help will be very much appreciated!Thanks
'Values' is a keyword in SQL so you can't use it as a variable name. Change it to 'value' or something else in the data frame, that should sort the SQL error.
It looks like you're following the example on http://www.r-chart.com/2010/07/pie-charts-in-ggplot2.html.
Firstly, you have y = Summary in your ggplot, that needs to be updated to 'value' for your code.
Next, there seemed to be an issue with the data you're using (I don't have outOfMem1noIns so i made test data), but you should make sure the values for each MorT sum up to 1.
Then the code as it is on the tutorial page should work (maybe with some warning messages...)
The SQL statement has a syntax error, as the error states. In addition, the ggplot2 error comes from the fact that you have not added a geometry, e.g. geom_point:
p = ggplot(data=df,
aes(x=factor(1),
y=Summary,
fill = factor(Model)
)
) + geom_point()

Resources