brittAnderson / Intro2Computing4Psychology

A guided introduction to computing tools useful for research in psychology - targeted to complete beginners
46 stars 159 forks source link

ggplot doesn't seem to want to function #86

Closed flwcaron closed 2 years ago

flwcaron commented 2 years ago

I'm going currently going through the videos for the plotting topic, but for some reason I haven't been able to get ggplot to work. Importing the ggplot library doesn't give me any issues, so I believe I should have it, but for some reason even if I copy and paste in the code from the topic, I can't get ggplot to work the way the example for base plot does. I can't get it to display inline, the file doesn't seem to exist, and when I click on the link to it in emacs, it just opens with a grey bar at the top with a bunch of numbers and letters and an otherwise blank buffer. Any idea what's happening here?

Thanks!

brittAnderson commented 2 years ago

You will want to debug what is ggplot and what is emacs. Try a simple terminal version and see if that works. If not the problem is ggplot. For example, in a terminal I type R that starts an R interpreter. Than library(ggplot2) qplot(c(1,2,3),c(2,3,4),geom="line")

That should pop open a window with a line diagonally. Does that work?

I so you can cut and paste into an org source block and C-c C-c as a test case.

After you compile the block you can see the file locally with C-c C-x C-v which is a short cut for toggle inline images. The same command again hides it.


    #+Title:test ggplot

    #+begin_src R :session *test* :results file graphics :exports both :file ./test.png
      library(ggplot2)
      qplot(c(1,2,3),c(2,3,4),geom="line")
    #+end_src

Report back your error messages and give a minimal example of the code that fails.

flwcaron commented 2 years ago

So it does seem to work, but after messing with the original plot a bit I've noticed that for whatever reason, the error it's giving me is "object 'x' not found", even though I used the same session name as the first portion. Here's the code I'm working with, which for all intents and purposes should be the same as the example from the topics:

`#+begin_src R :session RPlot x <- 1:10 y1 <- x^2 y2 <- 2*y1 y3 <- rnorm(10)

+end_src`

`#+begin_src R :session RPlot :results file replace graphics :exports both :file "~/Desktop/ggplot1.png" library(ggplot2) p <- ggplot(data = data.frame("x" = x, "y1" = y1, "y2" = y2, "y3" = y3), aes(x = x, y = y1, col= 'r')) p <- p + geom_point() + geom_line() + theme(legend.position = c(0.2,0.65)) + geom_line(aes(x=x,y=y2, col = "blue")) + geom_line(aes(y=y3,col = "green")) ggsave("ggplot1.png", width = 8, height = 5, units = "cm")

+end_src`

flwcaron commented 2 years ago

For what it's worth, I tried going ahead and trying the python plots too, and those don't work either even when copy and pasting the code for those. After a lot of just trying it over and over, the first one creates a plot but with not legend or points or lines, just the outline of the plot and what seems like it could be a little grey circle or square in the top left of the otherwise empty outlined box with an x and y axis.

brittAnderson commented 2 years ago

This works for me (to upload I had to change .org to .txt; you can change it back). You need to watch your line breaks. Python cares even more about white space. Also, if you specify the output of the source block is going to be in your desktop you should save the file there.

ggplot1 test.txt

flwcaron commented 2 years ago

Sorry I don't know why it posted like that in the comment, but I did have proper line breaks. It still does not work. Even changing it to the directory where the actual file is just brings me back to the same previous error - "object x not found". I'm starting to think it has to do with my Emacs but I don't understand what the problem could be.

flwcaron commented 2 years ago

On the other hand, the error I was getting with the python plot was "Unknown image type; consider switching 'image-use-external-converter' on", which I did, but now it still doesn't work and just opens a blank buffer when I click the file link. I'm extremely confused at this point.