<p>As you seem to have surmised, the graph is not erroneous, but correct. You have a total of 10 for <code>a</code> and that's what it's showing. If you want it to ignore duplicate X-axis values then you need to tell ggplot which datapoints it should actually be using. <code>barplot()</code> is assuming that your duplicate <code>a</code> values are intentional, ggplot assumes they should be grouped. These are both reasonable assumptions and it's your responsibility to determine beforehand which is correct.</p>

CC BY-SA 4.0
2020-12-21T14:47:06.927Z
2020-12-21T14:47:06.927Z
Devon Ryan
<p>I am plotting a barplot using <code>ggplot</code> + <code>geom_bar()</code> function in R.</p>
<p>I have some the recoreds which share the same x-axis identifiers.</p>
<p><code>geom_bar()</code> did merge all values that shared the same x-axis identifiers.</p>
<p>Like the following example.</p>
<pre><code>library(&quot;ggplot2&quot;)

df &lt;- data.frame(x = c(&quot;a&quot;,&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;e&quot;),
                 y = c(5,5,5,5,5,5),
                 stringsAsFactors = FALSE)

ggplot(df, aes(x, y)) + geom_bar(stat = &quot;identity&quot;)

</code></pre>
<p>The erroneous graph is displayed below.</p>
<p>The highest bar means the sum of multiple y-values shared same x-axis names.</p>
<p>Anyone else could help me to solve this problem? Thanks.</p>
<p><a href="https://i.sstatic.net/LnPrH.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/LnPrH.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2020-12-20T07:20:51.083Z
Please provide a reproducible example so we can understand what's going on.
Ram RS
CC BY-SA 4.0
2020-12-20T20:30:28.223Z
Interesting question. Your data is out of shape though - you cannot plot duplicate data points on an axis - how will anyone reading the plot know the difference between the two data points?
Ram RS
CC BY-SA 4.0
2020-12-21T02:34:21.800Z
Finally， I solved it using `barplot()` function that does not merge values that have same x-axis name.
YP CHEN
<r><ggplot2>
ggplot2 geom_bar() , do not merge y-values shared same x-ids
# ggplot2 geom_bar() , do not merge y-values shared same x-ids

(Asked by: username_0 on 2020-12-20)

I am plotting a barplot using ggplot + geom_bar() function in R.
I have some the recoreds which share the same x-axis identifiers.
geom_bar() did merge all values that shared the same x-axis identifiers.
Like the following example.
library("ggplot2")

df <- data.frame(x = c("a","a","b","c","d","e"),
                 y = c(5,5,5,5,5,5),
                 stringsAsFactors = FALSE)

ggplot(df, aes(x, y)) + geom_bar(stat = "identity")


The erroneous graph is displayed below.
The highest bar means the sum of multiple y-values shared same x-axis names.
Anyone else could help me to solve this problem? Thanks.

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: Please provide a reproducible example so we can understand what's going on.  
> **username_1**: Interesting question. Your data is out of shape though - you cannot plot duplicate data points on an axis - how will anyone reading the plot know the difference between the two data points?  
> **username_0**: Finally， I solved it using `barplot()` function that does not merge values that have same x-axis name.  

---

## 1 answers

### Answer by username_2 on 2020-12-21. Score: 1

As you seem to have surmised, the graph is not erroneous, but correct. You have a total of 10 for `a` and that's what it's showing. If you want it to ignore duplicate X-axis values then you need to tell ggplot which datapoints it should actually be using. `barplot()` is assuming that your duplicate `a` values are intentional, ggplot assumes they should be grouped. These are both reasonable assumptions and it's your responsibility to determine beforehand which is correct.


---

