[英]How to draw a graph in R language with labels in X-axis
I have the following data in .csv file: 我在.csv文件中包含以下数据:
Name marks1 marks2
xy 10 30
yz 20 40
zx 30 40
vx 20 20
vt 10 20
How do I draw a graph with both marks1
and marks2
in y-axis and name in x-axis? 如何在y轴上同时
marks1
和marks2
以及在x轴上命名的图形?
y <- cbind(data$marks1,data$marks2)
x <- cbind(data$Name)
matplot(x,y,type="p")
Here is one possibility using ggplot: 这是使用ggplot的一种可能性:
## Creating your dataset
Name <-c("xy","yz","zx","vx","vt")
marks1 <- c(10,20,30,20,10)
marks2 <- c(30,40,40,20,20)
## Combine the data into a data frame
data <- data.frame(Name,marks1,marks2)
## Loading libraries
library(ggplot2)
library(reshape2)
## Reshape the data from wide format to long format
redata <- melt(data,id="Name")
redata
## plot your data
ggplot(redata,aes(x=Name,y=value))+geom_point(aes(color=variable))+ylab("Marks")
The output is as follows: 输出如下:
Updated for the file read 已更新读取的文件
If you have a file with the above data then you can read your data using the following command: I am assuming your file has a extension *.csv and is spaced by commas. 如果您的文件包含上述数据,则可以使用以下命令读取数据:我假设您的文件扩展名为* .csv,并以逗号分隔。
data <- read.table("mydata.csv",header=T,sep=",")
or you directly use the following code for csv files 或者您直接将以下代码用于csv文件
data <- read.csv("mydata.csv")
After that you can go to libraries part in my above answer. 之后,您可以转到我上面的答案中的库部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.