Multiple values ​​in each row with a scatter plot in R

I have the following input file for R:

car 1 car 2 car 3 car2 1 car2 2 car2 3 

Then I use the following commands to plot the graph:

autos_data <- read.table ("~ / Documents / R / test.txt", header = F)

dotchart (autos_data $ V2, autos_data $ V1)

But this displays the value of each car and car2 on a new line, how can I plot so that all the values ​​of the car are on one line, and all the values ​​of car2 are on another line.

+4
source share
3 answers

As far as I can tell, there is simply no way to do this with the dotchart base.

However, if the dotplot grid meets your needs, you can simply do:

 library(lattice) dotplot(V1~V2, data=autos_data) 

enter image description here

+5
source

Please note that you can add points to the scatter plot using ? points , so this can be done in the R database with a bit of data management. Here's how to do it:

 autos_data = read.table(text="car 1 car 2 car 3 car2 1 car2 2 car2 3", header=F) aData2 = autos_data[!duplicated(autos_data[,1]),] dotchart(aData2[,2], labels=aData2[,1], xlim=c(min(autos_data[,2]), max(autos_data[,2]))) points(autos_data[,2] , autos_data[,1]) 

enter image description here

Josh O'Brien's lattice solution is, of course, more elegant.

+5
source

Here a ggplot2 solution

 qplot(V1, V2, data=autos_data) + coord_flip() 

enter image description here

+1
source

Source: https://habr.com/ru/post/1380589/


All Articles