# R Error in barplot.default() : ‘height’ must be a vector or a matrix (Example)

In this article you’ll learn how to debug the “Error in barplot.default() : ‘height’ must be a vector or a matrix” in the R programming language.

You’re here for the answer, so let’s get straight to the examples.

## Example 1: Reproduce the Error in barplot.default() : ‘height’ must be a vector or a matrix

This example illustrates how to replicate the error message in the barplot() function: “‘height’ must be a vector or a matrix”.

For this, we first have to create some example data:

```data <- data.frame(x = LETTERS[1:5],    # Create example data
y = 5:1)
data                                    # Print example data``` In Table 1 it is shown that we have created a data frame consisting of two columns. The variable x contains the x-axis labels and the variable y contains the height values of our bars.

Let’s try to draw these data with the barplot function:

```barplot(data)                           # Try to create barplot
# Error in barplot.default(data) : 'height' must be a vector or a matrix```

As you can see, the previous R code has returned the “Error in barplot.default(data) : ‘height’ must be a vector or a matrix”.

The reason for this is that we have inserted a data.frame object into the barplot function, i.e. we have specified the height to be equal to our data frame.

However, the height argument of the barplot function only takes numeric vectors or matrices as input.

So how can we solve this problem? That’s what I’ll show next!

## Example 2: Fix the Error in barplot.default() : ‘height’ must be a vector or a matrix

This example shows how to draw a bargraph in R without returning the “Error in barplot.default() : ‘height’ must be a vector or a matrix”.

In order to do that, we have to modify our input data as shown below:

```data_bar <- data\$y                      # Extract values
names(data_bar) <- data\$x               # Assign names to values
data_bar                                # Show new data
# A B C D E
# 5 4 3 2 1```

The previous output of the RStudio console shows how our data looks like. It is a named vector object consisting of the numeric height values and the labels as names of this vector.

Now, we can insert this named vector into the barplot function:

`barplot(data_bar)                       # Draw barplot with names` After executing the previous R programming syntax the barplot shown in Figure 1 has been plotted.

## Video & Further Resources

I have recently published a video on my YouTube channel, which explains the contents of this page. You can find the video below:

In addition, you might have a look at some of the related articles of this homepage:

At this point you should have learned how to handle the “Error in barplot.default() : ‘height’ must be a vector or a matrix” in the R programming language. If you have additional questions or comments, please let me know in the comments below.

Subscribe to the Statistics Globe Newsletter
{"ft_lang_label":"__label__en","ft_lang_prob":0.7719487,"math_prob":0.8159643,"size":3080,"snap":"2023-40-2023-50","text_gpt3_token_len":739,"char_repetition_ratio":0.16092327,"word_repetition_ratio":0.14003591,"special_character_ratio":0.24058442,"punctuation_ratio":0.10433387,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9889116,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-09T05:37:16Z\",\"WARC-Record-ID\":\"<urn:uuid:0b17f0cc-3cee-4821-a29b-bd124a4c07e4>\",\"Content-Length\":\"211945\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:56626ea7-f41b-4233-ac78-0661a18b3060>\",\"WARC-Concurrent-To\":\"<urn:uuid:a4645e1c-79cd-4b16-9389-65b5c76eceb3>\",\"WARC-IP-Address\":\"217.160.0.159\",\"WARC-Target-URI\":\"https://statisticsglobe.com/r-error-in-barplot-height-must-be-vector-or-matrix\",\"WARC-Payload-Digest\":\"sha1:CY2CCYFS4GQC75XSFGFGSL66E3ZCPVSP\",\"WARC-Block-Digest\":\"sha1:FNGQ3CJYOBQXLGGQUTPJ5S2RXYQHGA3X\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100800.25_warc_CC-MAIN-20231209040008-20231209070008-00146.warc.gz\"}"}