0

I was trying to plot using ggplot but I am a beginner. The data frame (newdata) contains 2002 rows and 3 columns. When I use basic graphics plot function I get a perfect plot like an inverted bell shape but when I use ggplot2 I don't get a similar plot. (i solved it..my dataframe had character columns,i converted them to numerical values and i plotted it..i works fine now..) now i have two plots val vs dat1 and val vs dat2 , now i need to combine them using facet wrap...any help would be appreciated..

val = -1000,-999,-998 to 998,999, 1000.
dat1 = 1.2, 3.4, 5.5, 33.3, 55.4, ... so on. dat2 is similar to y values.

str(newdata)
'data.frame':   2001 obs. of  3 variables:
 $ val  : chr  "-1000" "-999" "-998" "-997" ...
 $ dat1     : num  0.229 0.235 0.247 0.25 0.249 ...
 $ dat2   : num  1.97 1.98 1.98 1.98 1.98 ...

ggplot(data = newdata) + 
  geom_point(mapping = aes(x = val, y = dat1))

updated..dput(data) since the data is very huge im adding a part of it.

structure(list(**val** = c("-1000", "-999", "-998", "-997", 
"-996", "-995", "-994", "-993", "-992", "-991"), dat1 = c(0.229377104377104, 
0.23526936026936, 0.246843434343434, 0.250210437710438, 0.248526936026936, 
0.252314814814815, 0.226641414141414, 0.230218855218855, 0.223484848484848, 
0.236952861952862),  dat2 = c(1.97385049452862, 
1.97675496296296, 1.97780065909091, 1.97756823063973, 1.97745205218855, 
1.98053092087542, 1.98291262079125, 1.98401634175084, 1.98796655597643, 
1.98639806102694)), row.names = c(NA, 10L), class = "data.frame")
3
  • 2
    Please share at least a sample of your data, preferably with either (1) literal code to produce the frame (assuming no underlying CSV or other files required); or (2) the output from dput(head(newdata)), with enough data to be able to demonstrate plotting the data. (edit your question and post it there in a code-block, not here in a comment, please!) Commented Jun 12, 2019 at 21:45
  • 2
    Can you post sample data in dput format? Please edit the question with the output of dput(newdata). Or, if it is too big with the output of dput(head(newdata, 20)). Commented Jun 12, 2019 at 21:46
  • 1
    updated dput(data) Commented Jun 13, 2019 at 0:13

2 Answers 2

1

Try:

ggplot(data = newdata, aes(x = val, y = dat1)) + geom_line()

Sign up to request clarification or add additional context in comments.

1 Comment

(i solved it..my dataframe had character columns,i converted them to numerical values and i plotted it..i works fine now..)
0

To facet, you need data in long format (see this paper for a description of "tidy" data). Basically, you need one column that holds values of either dat1 or dat2, and another column that distinguishes between rows for dat1 and rows for dat2.

Here's how to accomplish this for your dataset:

library(dplyr)
library(tidyr)
library(ggplot2)

# Convert from wide to long.  Convert strings to numbers.
data.long = newdata %>%
  gather(dat.type, dat.value, -val) %>%
  mutate(val = as.numeric(val))

# Plot and facet.
ggplot(data.long, aes(x = val, y = dat.value)) +
  geom_line() +
  facet_wrap(~ dat.type)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.