Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8827865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:28:28+00:00 2026-06-14T07:28:28+00:00

I am trying to plot a vector data in a histogram-like style. This is

  • 0

I am trying to plot a vector data in a histogram-like style. This is always kind of easy, because plot(data, type="h") does exactly what I want. However, there is a problem with the color. My vector data looks like this:

 data = c(1,2,2,3,1,1,2,3,1,2,2,3, ... )

What I want to see, is that each 1 is plotted in one color, each 2 in a different color and each 3 likewise. I tried to achieve that with

 plot(data, type="h", col=c("red","blue","green")

but it failed with R looping over the color vector, so that the first bar was red, the second blue, the third green, the fourth red again and so on.

I am very interessted in general solution, because my data vectors do not always consist of the numbers 1, 2 and 3. There are many cases in which the vector holds the numbers from 1 to 6.

Does anybody know how to solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:28:30+00:00Added an answer on June 14, 2026 at 7:28 am

    You would need to create a vector of colors of the same length, like this:

    data = c(1,2,2,3,1,1,2,3,1,2,2,3)
    colors = c("red","blue","green")
    plot(data, type="h", col=colors[data])
    

    This works because colors[data] looks like:

    print(colors[data])
    #  [1] "red"   "blue"  "blue"  "green" "red"   "red"   "blue"  "green" "red"  
    # [10] "blue"  "blue"  "green"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to plot this kind of behaviour of a data set. I
I am trying to plot some test data (show at the end of this
I am trying to plot some categorical data and this answer is very close
I'm trying to make a histogram of a vector using R. When I plot
I am trying plot data sets consisting of 3 coordinates: X-coordinate, x-coordinate and the
I am trying to plot some data using pylab scatter function. I'm using pylab.scatter(X,
I'm trying to plot 6 days of intraday data as 6 charts. Quantmod's experimental
I get this horrible massive error when trying to plot using matplotlib: Traceback (most
I've run into a bit of a hiccup trying to plot some data in
I'm trying to plot some cities on Google's Geochart. This function works perfectly fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.