Reputation: 603
I'm trying to compare base R plot to ggplot to see if I get the reproducible figure, the plot is simple barplot wit legend, but when I run the code, I get an error, my data here:
structure(list(rated = c(FALSE, TRUE, TRUE, TRUE), turns = c(13L,
16L, 61L, 61L), victory_status = structure(c(2L, 3L, 1L, 1L), .Label = c("mate",
"outoftime", "resign"), class = "factor"), winner = structure(c(2L,
1L, 2L, 2L), .Label = c("charcoal", "cream"), class = "factor"),
increment_code = structure(c(1L, 3L, 3L, 2L), .Label = c("15+2",
"20+0", "5+10"), class = "factor"), cream_rating = c(1500L,
1322L, 1496L, 1439L), charcoal_rating = c(1191L, 1261L, 1500L,
1454L), opening_name = structure(c(4L, 2L, 1L, 3L), .Label = c("King's Pawn Game: Leonardis Variation",
"Nimzowitsch Defense: Kennedy Variation", "Queen's Pawn Game: Zukertort Variation",
"Slav Defense: Exchange Variation"), class = "factor")), row.names = c(NA,
4L), class = "data.frame")
I wanted to compare between Base R and ggplot function, I'm new to ggplot, I used the following:
dfg<-read.csv("./data/games.csv")
d<-table(dfg$winner, dfg$rated)
nd.1<-data.frame(dfg)
barplot(dwin, col=c("blue", "red", "green"))
legend("topleft", legend=rownames(d), col("blue", "red", "green"), pch= 16)
ggplot(nd.1, aes(fill=Var1), y=Freq, x=Var2) +
geom_bar(position="stack", stat="identity")
I'm getting this error:
ERROR while rich displaying an object: Error in FUN(X[[i]], ...): object 'Var1' not found
Traceback:
1. FUN(X[[i]], ...)
2. tryCatch(withCallingHandlers({
. if (!mime %in% names(repr::mime2repr))
. stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
. rpr <- repr::mime2repr[[mime]](obj)
. if (is.null(rpr))
. return(NULL)
. prepare_content(is.raw(rpr), rpr)
. }, error = error_handler), error = outer_handler)
3. tryCatchList(expr, classes, parentenv, handlers)
4. tryCatchOne(expr, names, parentenv, handlers[[1L]])
5. doTryCatch(return(expr), name, parentenv, handler)
6. withCallingHandlers({
. if (!mime %in% names(repr::mime2repr))
. stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
. rpr <- repr::mime2repr[[mime]](obj)
. if (is.null(rpr))
. return(NULL)
. prepare_content(is.raw(rpr), rpr)
. }, error = error_handler)
7. repr::mime2repr[[mime]](obj)
8. repr_text.default(obj)
9. paste(capture.output(print(obj)), collapse = "\n")
10. capture.output(print(obj))
11. evalVis(expr)
12. withVisible(eval(expr, pf))
13. eval(expr, pf)
14. eval(expr, pf)
15. print(obj)
16. print.ggplot(obj)
17. ggplot_build(x)
18. ggplot_build.ggplot(x)
19. by_layer(function(l, d) l$compute_aesthetics(d, plot))
20. f(l = layers[[i]], d = data[[i]])
21. l$compute_aesthetics(d, plot)
22. f(..., self = self)
23. scales_add_defaults(plot$scales, data, aesthetics, plot$plot_env)
24. lapply(aesthetics[new_aesthetics], eval_tidy, data = data)
25. FUN(X[[i]], ...)
What did it go wrong?
Upvotes: 0
Views: 577
Reputation: 39623
Maybe you have to place all the elements inside aes()
like this. The issue is that most of the aesthetics elements for the plot were outside aes()
. Placing them inside the function will produce next output (I have used as dfg
the data you shared. The data for the first plot was not present so I could not reproduce it). Here the code for the ggplot2
visualization:
library(ggplot2)
#Data
d<-table(dfg$winner, dfg$rated)
nd.1<-data.frame(d)
#Plot
ggplot(nd.1, aes( y=Freq, x=Var2,fill=Var1),) +
geom_bar(position="stack", stat="identity")
Output:
Upvotes: 1