ggplot2画柱状图

柱状图一般有三种形式:

  • 填充式
  • 堆积式
  • 并列式

填充式

1)填充式需要设定几何对象geom_bar()的参数position='fill'(堆叠数据,百分比展示)

首先数据格式是这样的:

ggplot2画柱状图_第1张图片

 

library(ggplot2)

data<-data.frame(Sample<-c(rep('YDY019',3),rep('YDY022',3),rep('YDY069',3),rep('YDY106',3),rep('YDY109',3),rep('YDY124',3),rep('YDY125',3)),
                   contion<-rep(c('Con','Tissue','Organ'),7),
                   value<-c(503,264,148,299,268,98,363,289,208,108,424,353,1,495,168,152,367,146,48,596,143))
#给每列命名
colnames(data)[1:3]<-c("Sample","condition","Cancer-related varivants")

#画图
ggplot(data, aes(Sample,value, fill=condition))+geom_bar(stat='identity',position='fill') +labs(x = 'Specie',y = 'Cancer-related varivants') +theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'))

结果图:

ggplot2画柱状图_第2张图片

 

ggplot:

Usage

ggplot(data = NULL, mapping = aes(), ..., environment = parent.frame())

Arguments

data

Default dataset to use for plot. If not already a data.frame, will be converted to one by fortify(). If not specified, must be supplied in each layer added to the plot.

mapping

Default list of aesthetic mappings to use for plot. If not specified, must be supplied in each layer added to the plot.

...

Other arguments passed on to methods. Not currently used.

environment

DEPRECATED. Used prior to tidy evaluation.

 

geom_bar

Usage

geom_bar(mapping = NULL, data = NULL, stat = "count",
  position = "stack", ..., width = NULL, binwidth = NULL, na.rm = FALSE,
  show.legend = NA, inherit.aes = TRUE)

geom_col(mapping = NULL, data = NULL, position = "stack", ...,
  width = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE)

stat_count(mapping = NULL, data = NULL, geom = "bar",
  position = "stack", ..., width = NULL, na.rm = FALSE,
  show.legend = NA, inherit.aes = TRUE)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE(the default), it is combined with the default mapping at the top level of the plot. You must supplymapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

function will be called with a single argument, the plot data. The return value must be adata.frame., and will be used as the layer data.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

...

Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like color = "red" or size = 3. They may also be parameters to the paired geom/stat.

width

Bar width. By default, set to 90% of the resolution of the data.

binwidth

geom_bar no longer has a binwidth argument - if you use it you'll get an warning telling to you usegeom_histogram() instead.

na.rm

If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

geom, stat

Override the default connection between geom_bar and stat_count.

2)堆积式需要设定几何对象geom_bar()的参数position = 'stack'(堆叠数据,非百分比展示)

#theme_bw()加框
ggplot(data,aes(Sample,value,fill=condition))+geom_bar(stat='identity', position='stack')+theme_bw() +labs(x = 'Sample',y = 'Value', title = 'This is barplot')+theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'),plot.title =element_text(hjust = 0.5, size = 20))

结果展示:

ggplot2画柱状图_第3张图片

3)并列式需要设定几何对象geom_bar()的参数position= 'dodge'(并列数据,非百分比展示)

ggplot(data,aes(x=Sample,y=value,fill=condition))+geom_bar(stat='identity', position='dodge') +theme_bw() +labs(x = 'Specie',y = 'Value', title = 'This is barplot') +theme(axis.title =element_text(size = 16),axis.text =element_text(size = 14, color = 'black'),plot.title =element_text(hjust = 0.5, size = 20))

ggplot2画柱状图_第4张图片

 

 

 

 

 

 

你可能感兴趣的:(R语言)