首页 > 解决方案 > R绘图函数中的“类型”参数

问题描述

我正在阅读 R 函数“情节”的一些教材,我已经有很多问题了。首先,我想了解“类型”参数是如何工作的以及何时工作。

我的代码:

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")

在第一个图中,当我尝试更改类型参数时,我得到相应更改的图形。像这样

在此处输入图像描述

但是,当我尝试对另一个真实数据集(见下文)执行相同操作时,它不起作用。数据集如下所示: 在此处输入图像描述

无论我尝试什么“类型”,它总是显示如下: 在此处输入图像描述

“类型”参数如何工作?谢谢

标签: rplot

解决方案


无论“类型”参数如何,您都看到相同类型的绘图的原因是数据集的“国家”列属于“因子”类型。

尝试将另一列作为“x”或定义x=as.numeric(countries)您将看到某些“类型”值是不允许的,而type="p"将显示带有点的图。


推荐阅读