Building a data frame in R

I'm new to R and I need some advice on building a DataFrame in R, which looks like this:

         V1          V2         V3          V4         
          1       Mazda     Toyota     Peugeot
   Car1.txt 0,507778837 0,19834711 0,146892655
   Car2.txt 0,908717802 0,64214047 0,396508728

I would like to build this data file (actually has 7 columns and 95 rows) in one graph, where v2, v3, v4 represent a line of a different color and are called car names, V1 as the label of the x axis, while the y axis is in the range [0,1].

I really have no clue how to do this, so I am very grateful for any advice

+3
source share
3 answers

The doctor prescribes a small modification of the Roman data frame.

library(ggplot2)
my.cars <- data.frame(
  Toyota = runif(50), 
  Mazda = runif(50), 
  Renault = runif(50),
  Car = paste("Car", 1:50, ".txt", sep = "")  
)

my.cars.melted <- melt(my.cars, id.vars = "Car")

He then suggests that it look like the car variable is categorical, so your first choice would be velvet.

p_bar <- ggplot(my.cars.melted, aes(Car, value, fill = variable)) +
  geom_bar(position = "dodge")
p_bar

, 95 . , -.

p_dot <- ggplot(my.cars.melted, aes(Car, value, col = variable)) +
  geom_point() +
  opts(axis.text.x = theme_text(angle = 90))
p_dot

, ( )

my.cars.melted$Car <- with(my.cars.melted, reorder(Car, value))

( p_dot, .)

, , ,

p_lines <- ggplot(my.cars.melted, aes(as.numeric(Car), value, col = variable)) +
  geom_line()
p_lines
+8

.

my.cars <- data.frame(Toyota = runif(50), Mazda = runif(50), Renault = runif(50)) #make some fake data for this example
plot(x = 1:nrow(my.cars), y = my.cars$Toyota, type = "n") #make an empty plot
with(my.cars, lines(x = 1:nrow(my.cars), y = Toyota, col = "red")) #add lines for Toyota
with(my.cars, lines(x = 1:nrow(my.cars), y = Mazda, col = "red")) # add lines for Mazda
with(my.cars, lines(x = 1:nrow(my.cars), y = Renault, col = "navy blue")) # add lines for Renault

with(), my.cars$Toyota, my.cars$Mazda... , . ?par , plot. ggplot2 .

+5

, , :

tcars <- read.table(textConnection(" V1       Mazda     Toyota     Peugeot
    Car1.txt 0,507778837 0,19834711 0,146892655
    Car2.txt 0,908717802 0,64214047 0,396508728", header=TRUE, dec=",")
 # need to use dec arg with commas as decimal points!
 tcars
        V1     Mazda    Toyota   Peugeot
1 Car1.txt 0.5077788 0.1983471 0.1468927
2 Car2.txt 0.9087178 0.6421405 0.3965087

 matplot(data.matrix(tcars[-1]), type="b", xaxt="n")
 axis(1, labels=tcars[[1]],at=1:NROW(tcars))

0
source

All Articles