Reputation: 91
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")
Upvotes: 0
Views: 76
Reputation: 2816
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)
Upvotes: 0
Reputation: 1820
Try:
ggplot(data = newdata, aes(x = val, y = dat1)) + geom_line()
Upvotes: 1