简体   繁体   English

R plot function 中的“类型”参数

[英]The 'type' parameter in R plot function

I am going through some teaching materials of the R function 'plot', and I already have tons of questions.我正在阅读 R function '情节'的一些教材,我已经有很多问题了。 To begin with, I would like to understand how the 'type' parameter works and when it works.首先,我想了解“类型”参数是如何工作的以及何时工作。

My code:我的代码:

t<-seq(from=0, to=10, by=0.1)
y<-sin(t)

#changing the type parameter here works, I can see the difference
#between, e.g., line or histogram
plot(x=t, y=y,
      type="h",
      xlab="Angle",
      ylab="Sine",
      col="red",
      main="Sine function")

rio <- read.csv(".../Rio2016.csv")
#see screenshot below for what the data look like
View(rio)
countries <- rio$Country
gold <- rio$Gold
silver <- rio$Silver
bronze <- rio$Bronze

# changing the type param here does not work. No matter
# what I tried, it always display the same thing
plot(x=countries,y=silver, type="h",
     xlab="Countries",
     ylab="Gold",
     col="red",
     main="Gold Medals")

In the first plot, when I tried to change the type parameter, I get the graphics changed accordingly.在第一个 plot 中,当我尝试更改类型参数时,我得到相应更改的图形。 Like this像这样

在此处输入图像描述

However, when I try to do the same for another real dataset (see below), it does not work.但是,当我尝试对另一个真实数据集(见下文)执行相同操作时,它不起作用。 Dataset looks like this:数据集如下所示: 在此处输入图像描述

No matter what 'type' I tried, it always displays like this:无论我尝试什么“类型”,它总是显示如下: 在此处输入图像描述

How does the 'type' parameter work? “类型”参数如何工作? Thanks谢谢

The reason you are seeing the same kind of plot regardless of the 'type' parameter is due to the fact that the 'Country' column of your dataset is of type 'factor'.无论“类型”参数如何,您看到相同类型的 plot 的原因是数据集的“国家”列属于“因子”类型。

Try taking another column as 'x' or define x=as.numeric(countries) you will see that some 'type' values are then not allowed while type="p" will display a plot with points.尝试将另一列作为 'x' 或定义x=as.numeric(countries)您将看到一些 'type' 值是不允许的,而type="p"将显示带有点的 plot。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM