条形图,也称为柱状图条形图,是一种以长方形的长度为变量的统计图表。
条形图可以是水平或垂直的,每个长方形可以有不同的颜色。
R 语言使用 barplot()
函数来创建条形图,格式如下:
barplot(H,xlab,ylab,main, names.arg,col,beside)
准备一个向量:
cvd19 = c(83534,2640626,585493)
显示条形图:
barplot(cvd19)
cvd19 = c(83534,2640626,585493)
barplot(cvd19,
main="新冠疫情条形图",
col=c("#ED1C24","#22B14C","#FFC90E"),
names.arg=c("中国","美国","印度"),
family='GB1'
)
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
Error in check_font_path(regular, "regular"): font file not found for 'regular' type Traceback: 1. check_font_path(regular, "regular") 2. stop(sprintf("font file not found for '%s' type", type)) 3. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "font file not found for 'regular' type", base::quote(check_font_path(regular, . "regular")))
设置文件名,输出为 png:
png(file = "runoob-bar-1.png")
加载字体:
showtext_begin();
colnames(cvd19) = c("中国", "美国", "印度")
rownames(cvd19) = c("6月", "7月")
barplot(cvd19, main = "新冠疫情条形图", beside=TRUE, legend=TRUE,
family='SyHei')
Error in `colnames<-`(`*tmp*`, value = c("中国", "美国", "印度")): attempt to set 'colnames' on an object with less than two dimensions Traceback: 1. stop("attempt to set 'colnames' on an object with less than two dimensions") 2. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "attempt to set 'colnames' on an object with less than two dimensions", . base::quote(`colnames<-`(`*tmp*`, value = c("中国", "美国", . "印度"))))
去掉字体:
showtext_end();
Error in showtext_end(): current device did not turn on showtext Traceback: 1. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "current device did not turn on showtext", base::quote(showtext_end()))
library(plotrix)
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
Error in check_font_path(regular, "regular"): font file not found for 'regular' type Traceback: 1. check_font_path(regular, "regular") 2. stop(sprintf("font file not found for '%s' type", type)) 3. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "font file not found for 'regular' type", base::quote(check_font_path(regular, . "regular")))
设置文件名,输出为 png:
png(file = "runoob-bar-2.png")
加载字体:
showtext_begin();
colnames(cvd19) = c("中国", "美国", "印度")
rownames(cvd19) = c("6月", "7月")
barplot(cvd19, main = "新冠疫情条形图", beside=TRUE,
legend=TRUE,col=c("blue","green"), family='SyHei')
Error in `colnames<-`(`*tmp*`, value = c("中国", "美国", "印度")): attempt to set 'colnames' on an object with less than two dimensions Traceback: 1. stop("attempt to set 'colnames' on an object with less than two dimensions") 2. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "attempt to set 'colnames' on an object with less than two dimensions", . base::quote(`colnames<-`(`*tmp*`, value = c("中国", "美国", . "印度"))))
去掉字体:
showtext_end();
Error in showtext_end(): current device did not turn on showtext Traceback: 1. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "current device did not turn on showtext", base::quote(showtext_end()))
library(showtext);
font_add("SyHei", "SourceHanSansSC-Bold.otf");
cvd19 = matrix(
c(83017, 83534, 1794546, 2640626, 190535, 585493),
2, 3
)
Error in check_font_path(regular, "regular"): font file not found for 'regular' type Traceback: 1. check_font_path(regular, "regular") 2. stop(sprintf("font file not found for '%s' type", type)) 3. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "font file not found for 'regular' type", base::quote(check_font_path(regular, . "regular")))
设置文件名,输出为 png:
png(file = "runoob-bar-3.png")
加载字体:
showtext_begin();
colnames(cvd19) = c("中国", "美国", "印度")
rownames(cvd19) = c("6月", "7月")
barplot(cvd19, main = "新冠疫情条形图", beside=FALSE,
legend=TRUE,col=c("blue","green"), family='SyHei')
Error in `colnames<-`(`*tmp*`, value = c("中国", "美国", "印度")): attempt to set 'colnames' on an object with less than two dimensions Traceback: 1. stop("attempt to set 'colnames' on an object with less than two dimensions") 2. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "attempt to set 'colnames' on an object with less than two dimensions", . base::quote(`colnames<-`(`*tmp*`, value = c("中国", "美国", . "印度"))))
去掉字体:
showtext_end();
Error in showtext_end(): current device did not turn on showtext Traceback: 1. .handleSimpleError(function (cnd) . { . watcher$capture_plot_and_output() . cnd <- sanitize_call(cnd) . watcher$push(cnd) . switch(on_error, continue = invokeRestart("eval_continue"), . stop = invokeRestart("eval_stop"), error = NULL) . }, "current device did not turn on showtext", base::quote(showtext_end()))
以下代码会在当前程序目录下生存一个 runoob-bar-3.png 文件,如下所示: