Horizontal line on ggplot2 barplot - r

I want a horizontal line to be placed over my ggplot2 bar plot at the average of all the bars:
ggplot(bar_graph, aes(x=V1, y=numBridges)) +
geom_bar(stat = "identity")+
xlab("Agreement")+
ylab("Number of Nodes")+
geom_hline(yintercept=15)
Unfortunately this code gives me the error message:
Error in is.finite(x) : default method not implemented for type 'list'
Which is super odd because 15 is not a list. It's also even more odd because, just to check, I ran the following code to make a vertical line - which worked well:
ggplot(bar_graph, aes(x=V1, y=numBridges)) +
geom_bar(stat = "identity")+
xlab("Agreement")+
ylab("Number of Bridge Nodes")+
geom_vline(xintercept=5)
A couple years ago there was a bug in ggplot2 that could only be resolved by installing directly from github, so in the event that this was still the problem I ran:
remove.packages('ggplot2')
devtools::install_github('hadley/ggplot2')
But that did not work either.
On Mac Monterrey:
packageVersion("ggplot2")
[1] ‘3.3.6.9000’
Does anyone have any ideas or workarounds? Would greatly appreciate any help!

Thanks to neilfws for pointing out that the column in my dataframe was indeed a list. I just did bar_graph$numBridges %>% unlist() to fix it.
I guess a nice reminder that a dataframe can be a cleverly hidden list!

Related

ggplot not working, error Error in grid.Call.graphics(C_setviewport, vp, TRUE)

I use ggplot regularly. I was rerunning some scripts tonight and for the first time am getting this error on every plot.
Error in grid.Call.graphics(C_setviewport, vp, TRUE) :
non-finite location and/or size for viewport
after checking my DFs nothing seemed wrong. I even ran a super basic line:
ggplot() + geom_point(aes(x = 1, y = 1))
and still get the same error.
can someone help?! perhaps it is a conflicting package or something? I am running ggplot version 3.3.2.
thank you!
Are you using RStudio? This happens sometimes to me when the Plots pane is too small to display the graph. Try enlarging it by dragging the sides and see if you're still getting the error?

troubleshooting object 'tibble_update_attrs' not found in R

I have been having troubles with R this morning, first reading in a .csv file, followed by trying to use GGPLOT2. To make sure it wasn't my code, I went to http://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization and grabbed the following code:
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
head(df)
library(ggplot2)
# Basic barplot
p<-ggplot(data=df, aes(x=dose, y=len)) +
geom_bar(stat="identity")
p
# Horizontal bar plot
p + coord_flip()
I can create the dataframe, but then get this error upon running the Basic Barplot code:
Error in update_tibble_attrs(x, ...) :
object 'tibble_update_attrs' not found
I updated packages, reinstalled GGPLOT2, restarted R-Studio and computer to no avail. Earlier today I was getting similar errors about tibbles when trying to build dataframes from scratch, and reading in .csv files.
I have Googled but have not found a relevant answer (or at least one that looks relevant).
How do I start troubleshooting this error?
Thanks!
I did the obvious and first used
traceback()
and learned that when using GGPLOT2 all this "tibble-stuff" goes on. I then:
install.packages("tibble")
and watched a number of errors fly by in the console. I then:
remove.packages("tibble")
install.packages("tibble")
and success! I now can make graphs in GGPLOT2 again.

Failing to create "flat violin plot" using geom_flat_violin()

I'm trying to use David Robinson's geom_flat_violin() to make a plot according to the example in this link: https://gist.github.com/dgrtwo/eb7750e74997891d7c20
ggplot(diamonds, aes(cut, carat)) +
geom_flat_violin() +
coord_flip()
If I follow the example I get
Error in geom_flat_violin() : could not find function "geom_flat_violin"
but when I try to copy in his function code, there are several "unexpected ',' " errors and I don't understand what to copy and what to leave behind. Can anyone help?
There are a number of issues with the code listed on the provided link. As the comments in the link suggested, there are some mismatched parenthesis. This can be fixed by removing line 50.
Also, be sure to install the libraries prior to running. This includes the ggplot2, dplyr, and plyr. Typically, they can be installed like so:
install.packages( c( "dplyr", "ggplot2", "plyr" ) )
I was able to run that code after making the fixes listed above.
source( './geom_flat_violin.R' )
ggplot( diamonds, aes( cut, carat ) ) + geom_flat_violin() + coord_flip()

Coloring points based on variable with R ggpairs

I'm trying to reproduce the figure in https://tgmstat.wordpress.com/2013/11/13/plot-matrix-with-the-r-package-ggally/ with the code
require(GGally)
data(tips, package="reshape")
ggpairs(data=tips, title="tips data", colour = "sex")
However, in the plot I get the points are not colored based on sex, instead they are all the same color. I get the following warning
Warning message:
In warn_if_args_exist(list(...)) :
Extra arguments: 'colour' are being ignored. If these are meant to be >aesthetics, submit them using the 'mapping' variable within ggpairs with >ggplot2::aes or ggplot2::aes_string.
I've tried adding ggplot2::aes(colour = sex), but that did not work either.
Does anyone else here have the same problem? I'm using R version 3.3.1 and GGally_1.2.0.
Thanks.
GGally has been under fairly rapid development, so it's not surprising that a blog post from 2013 has out-of-date code. When I run your code with GGally 1.2.0 I get the same warning. It works for me if I add the mapping:
require(GGally)
data(tips, package="reshape")
g1 <- ggpairs(data=tips, title="tips data",
mapping=ggplot2::aes(colour = sex),
lower=list(combo=wrap("facethist",binwidth=1)))
Following the wiki page for the wrap() incantation to stop complaints about needing to set binwidth in stat_bin ...

Color error in ggplot2 (Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification)

If I run:
library(ggplot2)
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
bp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
bp
I see the error: Error in grDevices::col2rgb(colour, TRUE) : invalid RGB specification.
If I run the identical code on my other computer, I get the expected plot. I am guessing that some plotting parameter is the culprit, but I have no idea how to find it. What's going wrong?
The computer that behaves strangely is Mac OS X 10_11_3 running R 3.2.2 via RStudio 0.99.489
Sorry I am late, but the problem is that the function alpha is masked form ggplot2, that's why if you restart R and happen to call alpha after sourcing psych but before ggplot2 it works.
Anyway, the way to solve it is to make explicit that we want the psych function:
reliability = psych::alpha(df)
Restarting R was sufficient to fix the weird behavior. Obviously should have tried that before posting...

Resources