Graphics Reference
In-Depth Information
Factors and character vectors behave similarly in ggplot2—the main difference is that with char-
acter vectors, items will be displayed in lexicographical order, but with factors, items will be dis-
played in the same order as the factor levels, which you can control.
Creating a Data Frame
Problem
You want to create a data frame from vectors.
Solution
You can put vectors together in a data frame with
data.frame()
:
# Two starting vectors
g
<-
c(
"A"
,
"B"
,
"C"
)
x
<-
1
:
3
dat
<-
data.frame(g, x)
dat
g x
A
1
B
2
C
3
Discussion
A data frame is essentially a list of vectors and factors. Each vector or factor can be thought of
as a column in the data frame.
If your vectors are in a list, you can convert the list to a data frame with the
as.data.frame()
function:
lst
<-
list(group
=
g, value
=
x)
# A list of vectors
dat
<-
as.data.frame(lst)
Getting Information About a Data Structure
Problem
You want to find out information about an object or data structure.